如何执行宏内的代码?

如何执行宏内的代码?

我使用 Michael Wichura 的旧表格宏(参见http://www.pctex.com/addons.html),我需要从另一个宏中访问它们。问题(我认为)是表宏中使用的某些字符未正确传递。让我用一些代码来说明。

\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}

我从该代码中获得的输出如下所示:

enter image description here

第一个表没有问题,但显然,当从 \ifstrequal 宏中调用表宏时会出现问题。事实上,当我从任何宏。

当然,我知道我可以使用更标准的 LaTeX 代码重建所有这些表格。问题是,在相当短的时间内,我需要重写大约 200 个这样的(整页)表格。

有没有更简单的方法来解决这个问题?

预先感谢您的帮助。

答案1

您可以使用\scantokens和一个命令,使输入不那么尴尬,处理更加灵活。

\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
$$}

\end{document}

enter image description here

另一种选择\scantokens

\makeatletter
\newcommand{\QS}{%
  \expandafter\ifstrequal\expandafter{\QuestionSolution}{S}{\@firstoftwo}{\@gobble}%
}
\makeatother

第二个参数实际上仅在 true 情况下才会被读取,此时将被丢弃。在“false”情况下,第一个参数将被丢弃,\@gobble并且表将被处理(在组内,但应该没有区别)。

答案2

当您使用像 这样的宏时\something{<x>}{<y>}{<z>},LaTeX 会读取所有参数和<x>,然后再处理 内部的内容。您说得对,类别代码会作为 的一部分进行更改<y><z>\somethingPiCTeX 的表格宏在读取参数后,这种情况就不会发生。因此,您需要采用不同的方法来处理这种情况,避免使用宏条件。

下面我使用了 pdfTeX 原语,如果等于 ,\pdfstrcmp{<one>}{<two>}则返回 的值。然后,您可以使用常规构造来判断相等性:0<one><two>\ifnum<pdfstroutput>=0 <T>\else <F>\fi

enter image description here

\documentclass[11pt]{article}

% These three lines read the macros
\input{pictable/nine}
\input{pictable/eight}
\input{pictable/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

\ifnum\pdfstrcmp{\QuestionSolution}{S}=0%
  This is the question
\else%
  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
 \]
\fi

\end{document}

答案3

这是另一种不使用 的方法\scantokens,并且它没有要删除的组括号(在某些情况下可能)。它不使用\ifstrequal任何一个,只使用普通的\if

(说实话这里的代码根本不是“在宏里面”)

\documentclass[11pt]{article}

%  \usepackage{etoolbox}  % not needed in this approach


\newcommand*{\QS}{\if\QuestionSolution S\expandafter\zaptoS\fi }

\newcommand{\bS}{}      % checking if already defined
% \S is \textsection so I chose \bS (begin S)

\long\def\bS #1\eS{}

\newcommand{\zaptoS}{} %checking if already defined
\long\def\zaptoS #1\bS{}

\newcommand*{\eS}{}   % (end S)


% These three lines read the macros
\input{pictable/base/nine}
\input{pictable/base/eight}
\input{pictable/base/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} % 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}

codes

相关内容