1

I'm trying to use pgffor to define a new command for \boldsymbol\mathsf like the following.

\documentclass{article}
\usepackage{pgffor,amsmath}

\newcommand\makemathsfbf[1]{ \foreach\a\b\c in{#1}{ \expandafter\gdef\csname\a\b\c\expandafter\endcsname\expandafter{% \expandafter\boldsymbol{\mathsf\expandafter{\a}} } } } \makemathsfbf{AAA,BBB,...,ZZZ} % {AAA,BBB, CCC, DDD, ..., ZZZ}

\begin{document} $\AAA$ % = $\boldsymbol{\mathsf A}$ \end{document}

I want to use the above command, i.e., \AAA, to replace \boldsymbol{\mathsf A} and so on. However, I could not make it to work. How should I modify it?

stackname
  • 137
  • First, it is not obvious what follows AAA. AAB? BBB? If I can't guess, how is the computer supposed to? As it stands, you should only be using \abc (or some other macro) instead of \a\b\c. – John Kormylo Sep 13 '21 at 13:43
  • @JohnKormylo You are right. I changed the list to make it more clear. Also, I tried \abc. Unfortunately, it didn't work out. – stackname Sep 13 '21 at 13:53
  • what John meant is that apart from other considerations AAA,BBB,...,ZZZ is not a valid loop specification for pgf (or any other language I know) – David Carlisle Sep 13 '21 at 13:58
  • Also, you only need \expandafter for \gdef so that you don't redefine \csname instead of the expanded version. – John Kormylo Sep 13 '21 at 14:00
  • \boldsymbol\mathsf{A} would be the same as \boldsymbol{\mathsf}{A} You are missing the {} for \boldsymbol but you want to loop from A to Z not an undefined sequence from AAA to ZZZ, then construct the csname from three copies of the loop variable – David Carlisle Sep 13 '21 at 14:01
  • expandafter\boldsymbol{ expands { which is not expandable, – David Carlisle Sep 13 '21 at 14:04
  • @DavidCarlisle :) :) Thanks. I made this from the answer https://tex.stackexchange.com/a/594722/181831. I think I need to go through a document to learn how to use this command. – stackname Sep 13 '21 at 14:10
  • For some reason the loop is treating AAA,BBB,...,ZZZ as a single entry rather than a list. – John Kormylo Sep 13 '21 at 14:17
  • @JohnKormylo pgffor treats the argument as a literal list unless the first item is "an incrementable thing" (which AAA isn't) – David Carlisle Sep 13 '21 at 14:23

2 Answers2

2

There is no way to define a loop AAA ... ZZZ you need A, ....Z but the pgf for loop isn't really helping here I would use

enter image description here

\documentclass{article}

\usepackage{bm,amsmath}

\makeatletter \count@=1 \loop \expanded{\noexpand\bmdefine\csname@Alph\count@@Alph\count@@Alph\count@\endcsname {\noexpand\mathsf{@Alph\count@}}} \ifnum\count@<26 \advance\count@@ne \repeat

\begin{document} $\mathsf{A}+\AAA$

$\mathsf{G}+\GGG$

\end{document}

David Carlisle
  • 757,742
1

This will run, but you will need to come up with a way to generate {AAA,BBB,...,ZZZ}. Maybe \Alph{\i}\Alph{\i}\Alph{\i}.

\documentclass{article}
\usepackage{amsmath}
\usepackage{pgffor}

\makeatletter \newcommand\makemathsfbf[1]{ \def\mylist{#1} \foreach \abc in \mylist { \expandafter\protected@xdef\csname\abc\endcsname{ \boldsymbol{\mathsf{\abc}} } } } \makeatother

\makemathsfbf{AAA,BBB,ZZZ} % {AAA,BBB, CCC, DDD, ..., ZZZ}

\begin{document} $\AAA$ \end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120