5

I have a tex document that contains sections that are automatically generated. These sections have strings that are observation IDs. I would like to have latex replace all occurrences of an ID with a string containing the star's name.

For example:

\caption{SED of \OID1342263516}

Would become:

\caption{SED of FR Tau}

My first thought was to have a macro, something like this:

\newcommand{\OID1342263516}{FR Tau}

However, I have learned that macros can't have numbers in their name. Is there a good way to go about doing what I want? Thanks.

  • 1
    I've got a solution I think, gimme 5 minutes :-) – yo' Feb 27 '15 at 23:29
  • Welcome to TeX.SX! Please help us to help you and add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – user31729 Feb 27 '15 at 23:34
  • 3
    It would be much easier if the generated code was \OID{1342263516} – egreg Feb 27 '15 at 23:35
  • @egreg Indeed, that's why it took me 3 minutes too long :-) – yo' Feb 27 '15 at 23:38
  • 1
    @equant: I hope, the Herschel did not observe more than 10^12 stars then ;-) – user31729 Feb 28 '15 at 0:02
5

It's with a bit of plain e-TeX to make it without the extra braces. The maximal value allowed here is 1073741823000, the minimal one is 1000 (or less if you make it at least 4 digits, I hope you fit in that. In can be extended by x digits, but only in case that all your numbers have at least x digits.

\documentclass{article}

\newcount\OIDcounta
\newcount\OIDcountb
\def\useOID{\csname OID\the\OIDcounta\the\OIDcountb\endcsname}
\protected\def\OID#1#2#3{\OIDcounta#1#2#3\afterassignment\useOID\OIDcountb}
\def\newOID#1#2{\expandafter\def\csname OID#1\endcsname{#2}}

\newOID{123456}{Tau 456 epsilon}

\begin{document}

\tableofcontents

\section{ABC \OID123456\ DEF}

Star number hundred twenty three is \OID123456\ for sure.

\typeout{\number\maxdimen}

\end{document}

If you're fine with the braces, then it's easy, and will work for even very large numbers (tens of digits):

\documentclass{article}

\makeatletter
\protected\def\OID#1{\@ifundefined{OID#1}{\GenericError{}{OID#1 not defined!}{}{}}{\csname OID#1\endcsname}}
\def\newOID#1#2{\expandafter\def\csname OID#1\endcsname{#2}}
\makeatother

\newOID{123456}{Tau 456 epsilon}

\begin{document}

\tableofcontents

\section{ABC \OID{123456} DEF}

Star number hundred twenty three is \OID{123456} for sure.

\end{document}
  • Wow, fast. Thanks. So if \OID{1342263516} is easier as suggested by egreg, could you show me how that method would look? – equant Feb 27 '15 at 23:48
  • Actually, the number 1342263516 is larger than 1073741823 and the macro still works. But there's no guarantee when the number is much larger. – egreg Feb 27 '15 at 23:53
  • @egreg I shoved off the first 3 digits now, so it works for anything from 1000 to a lot. – yo' Feb 27 '15 at 23:54
  • @equant Here you go. – yo' Feb 27 '15 at 23:56
  • Perfect. Both methods worked perfectly for me. The later is definitely easier to read. Thanks! – equant Feb 28 '15 at 0:02

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.