[Tex/LaTex] Having multiple commands with underscores and numbers that start with same text

catcodesexternal filesmacrosnaming

Recently me and some friends really got into "making everything compilable". More specifically, we need to do a lot of matlab calculations, and then we need to put the results into a tex document.

I've had some time left recently and decided to write a matlab script to output values and their uncertainty bounds to a file, in correct the correct form so they can be a called in LaTeX, with the correct number of significant digits and whatnot everything so no brains are needed. Let me show you an example:

1) I have a matlab script do.m that uses data form input files, processes it
and calculates what I want, say values that are stored in a and
b.

2) at the end of the script I call my savefortex script that saves variables a and b to a file, say out.txt, which will look like
this:

 \def\a{123}
 \def\b{456}

3) I put \input{out.txt} in my LaTeX document.

4) I setup my makefile so that it knows do.m should be called to have the latest out.txt.

This is very handy when it comas down to a large number of variables that have to be inputted. Recently I stumbled into a problem though. I usually name my variables an their corresponding uncertainty bound as follows: a, s_a, b, s_b, etc. Yet this is not compatible with Tex. Try this at home

\def\s_a{10}
\def\s_b{20}
\s_a and \s_b.

It simply won't work. The same goes for a1, a2, etc. Yet something like:

\def\s_a{10}
\s_a

does work! It seems as if tex doesn't allow for multiple macro's that have an underscore to have the same text before the underscore, even if the text after the underscore is different (again, this is the same with numbers in the macro names). So whilst s_a and s_b are okay to use separately, if they are both defined tex cannot use either of them!

What would be a good solution to this? Any suggestions for a different naming scheme? Or perhaps there's another way to get data from a file (this would be nice, since input takes a lot of time when there are a lot of macro's defined)?

I would also like to be able to use siunitx, so while \SI{\a+-\b}{\metre} works, \SI{\a+-\s_a}{\metre} does not. I would love it if it did.

Best Answer

There are two problems: the first is that after

\def\sa{10}
\def\sb{20}

the text \sa and \sb will render as

10and 20

because TeX ignores spaces after control words.

Second problem. A control sequence can consist either of

  1. only one non alphabetic character (control symbols); or
  2. a string of one or more alphabetic characters, that is [A-Za-z] (control words)

Spaces are not ignored after control symbols.

However definitions with \def allow for a fairly general syntax:

\def\cs<parameter text>{<replacement text>}

Here <parameter text> can be whatever sequence of tokens, including the argument indicators #1 to #9 to denote arguments. But if there are tokens different from the argument indicators, they must appear after \cs. So

\def\s_a{10}

is legal and defines a command \s which requires the two tokens _a to follow it. If you say after this

\def\s_b{20}

the command \s is redefined and usage of \s_a will trigger the error message

! Use of \s doesn't match its definition.

since \s is not followed by _b.

To solve both problems at once, use

\def\sa/{10}
\def\sb/{20}

and then \sa/ and \sb/ will render as

10 and 20

because the required / after the commands' names stops the space ignoring feature.

Related Question