# How to execute codes inside a macro?

I use Michael Wichura's old Table macros (see http://www.pctex.com/addons.html) in several places, and I need to access them from within another macro. The problem (I think) is that some of the characters that are used in the Table macros are not passed on correctly. Let me illustrate with some codes.

\documentclass[11pt]{article}

\usepackage{etoolbox}

% These three lines read the macros
\input{nine}
\input{eight}
\input{Table}

\begin{document}

This is a table: % The following few lines use the Table macros
$$\BeginTable \BeginFormat | lw8 | rw4 | rw4 | \EndFormat " Line 1 " 1.23 " 4.56 "\\ " Line 2 " 7.89 " 0.12 "\\ \EndTable$$

\def\QuestionSolution{Q} % This identifies whether I want a question or solution

\expandafter\ifstrequal\expandafter{\QuestionSolution}{S}{%
This is the question}{%
This is the solution with the same table:
$$\BeginTable \BeginFormat | lw20 | rw4 | rw4 | \EndFormat " Line 1 " 1.23 " 4.56 "\\ " Line 2 " 7.89 " 0.12 "\\ \EndTable$$}

\end{document}

The output I get from this code looks like this:

The first table is fine but, clearly, there is a problem when the table macros are invoked from within the \ifstrequal macro. In fact, the same problem occurs when I call the table from any macro.

Of course, I know that I could reconstruct all of these tables using more standard LaTeX codes. The problem is that I have about 200 such (full-page) tables to rewrite in a fairly short interval of time.

Is there an easier way to fix this problem?

• – Werner Sep 7 '13 at 0:44
• Actually @Werner, in this case I think $$is preferable because it is being used as a quick way to center the table (something that I have seen many plain tex users do) – Aditya Sep 7 '13 at 3:01 • @Aditya Well, in that case, \centerline is the correct option. – yo' Sep 7 '13 at 7:50 ## 3 Answers You can use \scantokens and a command that makes the input less awkward and the processing more flexible. \documentclass[11pt]{article} \usepackage{etoolbox} \newcommand{\QS}[2]{% \expandafter\ifstrequal\expandafter{\QuestionSolution}{S}{#1}{\scantokens{#2}}% } % These three lines read the macros \input{nine} \input{eight} \input{Table} \begin{document} This is a table: % The following few lines use the Table macros$$
\BeginTable
\BeginFormat
| lw8 | rw4 | rw4 |
\EndFormat
" Line 1  " 1.23 " 4.56 "\\
" Line 2  " 7.89 " 0.12 "\\
\EndTable
$$\def\QuestionSolution{Q} % This identifies whether I want a question or solution \QS{This is the question}{% This is the solution with the same table:$$
\BeginTable
\BeginFormat
| lw8 | rw4 | rw4 |
\EndFormat
" Line 1  " 1.23 " 4.56 "\\
" Line 2  " 7.89 " 0.12 "\\
\EndTable
\BeginTable
\BeginFormat
| lw8 | rw4 | rw4 |
\EndFormat
" Line 1  " 1.23 " 4.56 "\\
" Line 2  " 7.89 " 0.12 "\\
\EndTable
$$\def\QuestionSolution{Q} % I want a question \QS This is the question \bS This is the solution with the same table:$$
\BeginTable
\BeginFormat
| lw8 | rw4 | rw4 |
\EndFormat
" Line 1  " 1.23 " 4.56 "\\
" Line 2  " 7.89 " 0.12 "\\
\EndTable
$$\eS \def\QuestionSolution{S} % I now want here a solution \QS % question from here to \bS This is the question \bS % solution from here to \eS This is the solution with the same table:$$
\BeginTable
\BeginFormat
| lw8 | rw4 | rw4 |
\EndFormat
" Line 1  " 1.23 " 4.56 "\\
" Line 2  " 7.89 " 0.12 "\\
\EndTable

\eS

\end{document}

• I propose this answer because it allows inside the questions/solutions to use \if..\else..\fi constructs with no surprises, as there is no apparent conditional to TeX apart from the one processed in one go when \QS is executed. – user4686 Sep 7 '13 at 14:23
• I guess the more common situation is when one wants to display or not display the solutions (the questions being always printed). Then the mark-up needed would be only the \bS...\eS. One would have a switch \ifsolutions and define \def\bS{\ifsolutions\else\expandafter\zaptoeS\fi}, with \long\def\zaptoeS #1\eS {} and \def\eS {}. This way inside the solution code, arbitrary TeX code can be used with no possible surprise from TeX scanning conditional branches, compared to doing \ifsolutions <solution code>\fi. – user4686 Sep 7 '13 at 14:37
• Makes me think that the LaTeX mark-up \begin{solution}...\end{solution} does not make it easy to define the solution environment to annihilate conditionally its contents, seems this is exactly the job of package comment. – user4686 Sep 7 '13 at 14:48