I created a command to dynamically define vector macros:
\newcommand{\makevec}[1]{\expandafter\def\csname #1\endcsname{\vec{#1}}}
which works as I expected. But then I tried to modify this command to accept a comma separated list of symbols using the \foreach
command defined in the pgffor
package:
\newcommand{\makevec}[1]{
\foreach \name in {#1} {
\expandafter\def\csname \name\endcsname{\vec{\name}}
}
}
The command compiles without errors, but simply fails to define the correct macros. After a bit of Googling I ran across some questions somewhat similar to mine that suggested the issue may have to do with the scope, so I tried both:
\newcommand{\makevec}[1]{
\foreach \name in {#1} {
\global\expandafter\def\csname \name\endcsname{\vec{\name}}
}
}
and
\newcommand{\makevec}[1]{
\foreach \name in {#1} {
\expandafter\xdef\csname \name\endcsname{\vec{\name}}
}
}
The first of these yields the same results as my initial solution, while the second produces the errors Argument of \reserved@a has an extra }
and Paragraph ended before \reserved@a was complete
. Any help is appreciated.
UPDATE: Here's a minimal document:
\documentclass{article}
\usepackage{pgffor}
\newcommand{\makevec}[1]{
\foreach \name in {#1} {
\global\expandafter\def\csname \name\endcsname{\textbf{\name}}
}
}
\makevec{v}
\begin{document}
$\v$
\end{document}
This raises an undefined control sequence error
at the invocation of \v
and compiles to a blank document.
\noexpand\vec
. – user4686 Apr 26 '18 at 18:55\v
or other one letter commands for accents; you'll soon regret having done it. – egreg Apr 26 '18 at 20:38\makevec
command is so I can redefine the commands on a document by document basis, so that if I know I'm going to need a certain accent in a document I can simply not redefine it. – Kwarrtz Apr 26 '18 at 20:52