0

I would like to use markup for any math symbols/variables (Sorry, I'm not sure what is the correct terminology here). The reason is to make late changes of the naming convention easier by just adjusting in one place. The following minimal example shows what I want to do and also the limitation of using \newcommand as it does not allow to use numbers in the macro name. If the commented line is compiled it will throw an error, however numbers are needed to describe some variables as there will be multiple ones just different by the number.

\documentclass{scrbook}
\begin{document}
\newcommand{\asteer}{a_{steer}}
\newcommand{\awheelfl}{a_{Wheel,FL}}
\newcommand{\awheelfr}{a_{Wheel,FR}}
%\newcommand{\rthrottle5}{r_{Throttle,q5}}
\begin{equation}
    a_{Steer} = a_{Wheel,FL} - a_{Wheel,FR}
\end{equation}
\begin{equation}
    \asteer = \awheelfl - \awheelfr
\end{equation}
\end{document}

I found a solution here: https://texfaq.org/FAQ-linmacnames (#2), however I am wondering if there is a better solution to that. Maybe \newcommand doesn't need to be used for this at all.

Cheers, Lukas

  • The problem is not only that you usually can't define macros with numbers in them with \newcommand, you also can't use/call them like normal macros even if you do manage to define them: \rthrottle5 would just be interpreted as \rthrottle 5 (i.e. \rthrottle and then a 5). You could define your own 'replacement' for \newcommand that allows numbers in command names, but those commands would have to be called differently. The usual recommendation is just to let the numbers go and try Roman numerals or words, but you could also use an argument if that makes sense. – moewe Oct 30 '18 at 9:51
  • Solution number #3 from texfaq.org/FAQ-linmacnames is one of the more dangerous ones. I certainly would not recommend you do this in a more complex document. Especially if you have to turn more numbers into letters. – moewe Oct 30 '18 at 9:52
  • Command arguments seem to be a sensible solution here, as @moewe mentioned. Something like \awheel{FL}, \awheel{FR}, \rthrottle{5} with #1 in the body of the command at the position of FL/FR/5. – Marijn Oct 30 '18 at 10:08
  • @moewe: Sorry I accidentaly posted the wrong number. I was thinking of #2 beeing a practical but not very understandable solution. – Archer Oct 30 '18 at 10:12
  • Number 2 is what I thought of when I talked about a 'replacement' for \newcommand and calling the resulting macros differently (see also jfbu's answer below, you essentially have a call wrapper: instead of \foo5 you call \usename{foo5}). If you must have numbers and can't use an argument then that would be my preferred solution. But before you look into this I would urge you to look into commands with arguments or to drop the number in the macro name. – moewe Oct 30 '18 at 10:20
2

I would use glossaries. This gives you the additional benefit to build list of symbols.

\documentclass{scrbook}
\usepackage{glossaries}
\newglossaryentry{asteer}
{%
  name={$a_{\mathrm{steer}}$},
  text={a_{\mathrm{steer}}},
  description={whatever},
  sort={a}
}

\newglossaryentry{awheelfl}
{%
  name={$a_{\mathrm{Wheel,FL}}$},
  text={a_{\mathrm{Wheel,FL}}},
  description={whatever},
  sort={a}
}
\begin{document}
\begin{equation}
    \gls{asteer} = \gls{awheelfl}
\end{equation}

\end{document}

enter image description here

  • In this case wouldn't \scriptscriptstyle look better? – daleif Oct 30 '18 at 15:38
  • @daleif the look is up-to the user. – Ulrike Fischer Oct 30 '18 at 15:51
  • Who might not know about scriptscriptstyle – daleif Oct 30 '18 at 16:43
  • 1
    I like the benefit of automatic list of symbols hence I mark this answer as accepted. – Archer Oct 30 '18 at 16:45
2
\documentclass{article}

\makeatletter
\newcommand\newname[1]{\expandafter\newcommand\csname #1\endcsname}

\newcommand\usename[1]{\@nameuse{#1}}
\makeatother

\begin{document}

\newname{a1}{this is a1}

\usename{a1}

% \newname{a1}{this is a1}% LaTeX Error: Command \a1 already defined.

\newname{a2}[3]{this is a2, it gets arguments #1, #2, #3}

\usename{a2}{aa}{bbb}{cccc}

\end{document}

enter image description here


I realize now this is quite close to #2 of posted link. But it is better because it allows the star-variant to be defined too, as weel as macros with optional arguments.


updated

\documentclass{article}

\makeatletter
\newcommand\newname[1]{\expandafter\newcommand\csname #1\endcsname}

\newcommand\usename[1]{\@nameuse{#1}}

\newcommand\ZZZ{}% check not defined
\def\ZZZ#1#{\@nameuse{ZZZ#1}}

\makeatother

\begin{document}

\newname{a1}{this is a1}

\usename{a1}

% \newname{a1}{this is a1}% LaTeX Error: Command \a1 already defined.

\newname{a2}[3]{this is a2, it gets arguments #1, #2, #3}

\usename{a2}{aa}{bbb}{cccc}

\newname{ZZZ1}{this is ZZZ1, it has no arguments, braces mandatory}
\newname{ZZZ2}{this is ZZZ2, it has no arguments, braces mandatory}
\newname{ZZZ---999}[2]{this is ZZZ-\mbox{}-\mbox{}-999 with arguments #1 and #2}


\ZZZ1{}

\ZZZ2{}

\ZZZ---999{aaaa}{bbbb}

\end{document}

enter image description here

  • Why passing #1 from \usename to \@nameuse instead of \let\usename=\@nameuse or \newcommand*\usename{\@nameuse}? – Ulrich Diez Oct 30 '18 at 19:20
  • @UlrichDiez you are right \let\usename=\@nameuse is better. I wrote this in jest, not expecting it to be used by anyone, hopefully they will take this comment into account. – user4686 Oct 30 '18 at 19:54
0

In my answer to the question "Define a control sequence after that a space matters" I elaborated on a macro \name which is to be used as follows:

\name <tokens without braces>{<name of Control Sequence Token>}
<tokens without braces>\ControlSequenceToken

You can use the \name-macro both for defining and for calling macros.


Here comes the implementation:

\newcommand\name{}%
\long\def\name#1#{\romannumeral0\innername{#1}}%
%
\newcommand\innername[2]{%
  \expandafter\exchange\expandafter{\csname#2\endcsname}{ #1}%
}%
%
\newcommand\exchange[2]{#2#1}%

This is the way of using \name for defining macros:

Example 1:

\name\newcommand{rthrottle5} yields:
\newcommand\rthrottle5, thus you can, e.g., do:

\name\newcommand{rthrottle5}{r_{Throttle,q5}}
\newcommand\rthrottle5{r_{Throttle,q5}}

Example 2:

\name\global\long\def{foo} yields:
\global\long\def\foo, thus you can, e.g., do:

\name\global\long\def{foo}#1#2{foo's arg1: #1; foo's arg2: #2.}\global\long\def\foo#1#2{foo's arg1: #1; foo's arg2: #2.}


Example 3:

This is the way of using \name for calling macros: Just leave the <tokens without braces>-argument empty:

\name{rthrottle5}
\rthrottle5


The <tokens without braces>-argument can also consist of a sequence of some more calls of the name-macro.

Example 4:

\name\name\let{foo}={bar}
\name\let\foo={bar}
\let\foo=\bar

Example 5:

\name\name\name\futurelet{foo}{bar}{baz}
\name\name\futurelet\foo{bar}{baz}
\name\futurelet\foo\bar{baz}
\futurelet\foo\bar\baz


Using the \name-macro within your example:

\documentclass{scrbook}

\newcommand\name{}%
\long\def\name#1#{\romannumeral0\innername{#1}}%
\newcommand\innername[2]{%
  \expandafter\exchange\expandafter{\csname#2\endcsname}{ #1}%
}%
\newcommand\exchange[2]{#2#1}%

\begin{document}
\name\newcommand{asteer}{a_{Steer}}
\name\newcommand{awheelfl}{a_{Wheel,FL}}
\name\newcommand{awheelfr}{a_{Wheel,FR}}
\name\newcommand{rthrottle5}{r_{Throttle,q5}}
\begin{equation}
    a_{Steer} + r_{Throttle,q5} = a_{Wheel,FL} - a_{Wheel,FR} + r_{Throttle,q5}
\end{equation}
\begin{equation}
    \name{asteer} + \name{rthrottle5} = \name{awheelfl} - \name{awheelfr} + \name{rthrottle5}
\end{equation}

Or. if you like:

\begin{equation}
    \name\name\name\name\name%
    {asteer} + {rthrottle5} = {awheelfl} - {awheelfr} + {rthrottle5}
\end{equation}

\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.