2

I'm trying to write a little Latex package to learn more about Tex and to help me out with writing long and annoying proofs for a course by creating macros to retrieve rule names from a table given the rules number (which is given in a arbitrary compendium).

But I'm having severe problems with calling macros from within a macro.

This is my code:

%macro for formatting rules
\newcommand{\nR}[1]{%
=\;\{\text{{#1}}\}%
}

%Prints the rule specified by the argument from the nametable in a nice formatting
\newcommand{\Rnl}[1]{%
    &\\&\nR{\nT{{#1}}}\\&%
}
%rule nametable
\newcommand{\nT}[1]{%
    \stringcases
    {#1}%
    {%
      {1.1}{Axiom of whatever}%
      {2.1}{Silly theorem}%
    }%
    {\mathbf{UNDEFINED\_RULE\;\;{#1}}}% 
}

%I got these from:
% http://tex.stackexchange.com/questions/64131/implementing-switch-                            cases
\newcommand{\stringcases}[3]{%
  \romannumeral
    \str@case{#1}#2{#1}{#3}\q@stop
}
\newcommand{\str@case}[3]{%
  \ifnum\pdf@strcmp{\unexpanded{#1}}{\unexpanded{#2}}=\z@
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
    {\str@case@end{#3}}
    {\str@case{#1}}%
}
\newcommand{\str@case@end}{}
\long\def\str@case@end#1#2\q@stop{\z@#1}

But the problem is that when I call from a math environment \Rnl{1.1} I get a \mathbf allowed only in math mode. although if I change \mathbf to \textbf it will output {UNDEFINED_RULE 1.1}. Which it shouldn't do since there is a case matching 1.1.

The really confusing thing is that if i just call \nT{1.1} it will output "axiom of whatever" so that macro works as long as I don't call it from within another macro.

Below follows a compilable example of how I aim to use it:

\documentclass{article}  
\usepackage{amsmath}

%%Insert macros from above here.

\begin{document} 

What I want want to be able to write:  
\begin{align*}
   & A \wedge B \Rnl{1.1} Bob \Rnl{2.1} T               
\end{align*}
 What I want to get:
\begin{align*}
   &A \wedge B \\
   &=\;\text{\{The rule that coresponds to 1.1 in the name table\}} \\
   &Bob\\
   &=\;\text{\{The rule that coresponds to 2.1 in the name table\}} \\
   &T               
\end{align*}

\end{document}

How can I solve this?

  • 1
    Please, make a full compilable example. – egreg Oct 6 '15 at 12:34
  • Thanks @egreg That solved that issue! But I got a weird undefined control sequence but I'll look into that on my own. What is the difference between \DeclareRobustCommand and \newcommand ?? – Henrik Sommerland Oct 6 '15 at 13:10
  • @HenrikSommerland: If you think egreg's answer solves your troubles please consider to accept it. I can't see the relation to \DeclareRobustCommand here however. – user31729 Oct 9 '15 at 6:03
2

I see too many braces and \; in your code; the latter have no real consequences apart bad spacing, the additional braces are the key in understanding what goes wrong.

You have \Rnl{1.1} in your code, that, according to the definition, becomes

&\\&\nR{\nT{{1.1}}}\\&

The main part is, of course, \nR{\nT{{1.1}}} that becomes

=\;\{\text{{\nT{{1.1}}}}\}

and now \nT{{1.1}} becomes

\stringcases
  {{1.1}}%
  {%
    {1.1}{Axiom of whatever}%
    {2.1}{Silly theorem}%
  }%
  {\mathbf{UNDEFINED\_RULE\;\;{{1.1}}}}% 

(I kept the line breaks just for clarity). Can you see the problem? The token list to be tested against the cases is {1.1} that does not appear in the possible choices.

The error about \mathbf is because this is in \text where \mathbf is of course illegal.

Remove the additional braces and you'll be OK:

%macro for formatting rules
\newcommand{\nR}[1]{%
  =\{\text{#1}\}%
}

%Prints the rule specified by the argument from the nametable in a nice formatting
\newcommand{\Rnl}[1]{%
  \\
  &\nR{\nT{#1}}\\
  &
}
%rule nametable
\newcommand{\nT}[1]{%
  \stringcases{#1}%
    {%
      {1.1}{Axiom of whatever}%
      {2.1}{Silly theorem}%
    }%
    {\textbf{UNDEFINED\_RULE #1}}%
}

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.