有條件地定義(如果尚未定義)枚舉命令 \cmd1 \cmd2 ... 等的最佳方法是什麼?

有條件地定義(如果尚未定義)枚舉命令 \cmd1 \cmd2 ... 等的最佳方法是什麼?

我正在寫一份包含問題和答案的文檔。例如

\question{What is 2+3?}{2+3=5}
\question{What is cheese?}{Food made from milk.}

答案會被保存起來,以便在後面的部分中收集,但答案會被打亂以防止偷看。這意味著問題必須包含答案編號的前向引用。例如:

Q2.1 什麼是2+3?(見答案104)

Q2.2 什麼是起司?(見答案57)

我有這個幾乎在職的。

\question命令將所有答案寫入臨時文件,稍後的\printtheanswers命令將其發送到外部程序

A。打亂答案,

b.為答案部分生成 Latex,並且

C。產生一個輔助文件,其中qa.aux定義等來保存前向引用。\answernumber1\answernumber2

這是一個兩遍編譯。qa.aux如果檔案存在,則在開始時載入該檔案。該\question命令必須\answernumberN為每個不存在的命令進行定義,以擴展為佔位符。

目前,\question是部分定義的——它總是定義\answernumberN為佔位符:

\newcommand{\question}[2]{
    \stepcounter{questionnumber}
    \stepcounter{questionnumberinchapter}

    % Define the placeholder for \answernumberN
    \expandafter\edef\expandafter\answernumber\roman{questionnumber}{PLACEHOLDER:\arabic{questionnumber}}

    % FOR DEBUGGING
    DEFINING \texttt{\textbackslash answernumber\roman{questionnumber} as \{PLACEHOLDER:\arabic{questionnumber}\}}

    % Write "x.y #2" to \qainpfilename
    \immediate\write\qafile{\arabic{chapter}.\arabic{questionnumberinchapter} \unexpanded{#2}}

    % Print the qustion to the document.
    Q\arabic{chapter}.\arabic{questionnumberinchapter} #1 \emph{(See answer \expandafter\answernumber\roman{questionnumber}.)}
}

這是有興趣的線:

\expandafter\edef\expandafter\answernumber\roman{questionnumber}{PLACEHOLDER:\arabic{questionnumber}}

問題是this 總是定義\answernumberN,無論它是否在輔助檔案中定義。

這是一個 MWE,儘管沒有外部程式您實際上無法編譯它。

\documentclass[a4paper,10pt,openany]{scrbook}

\usepackage{verbatim}

% Define \bashline command
% Taken from https://gist.github.com/w495/7328b76e76aee49657e0bd7a3b46c870
\makeatletter
    \newcommand{\bashline@file@name}[1]{%
        /tmp/${USER}-${HOSTNAME}-\jobname-#1.tex%
    }
    \newread\bashline@file
    \newcommand{\bashline@command@one}[2][tmp]{%
        \immediate\write18{#2 > \bashline@file@name{#1}}
        \openin\bashline@file=\bashline@file@name{#1}
        % The group localizes the change to \endlinechar
        \bgroup
            \endlinechar=-1
            \read\bashline@file to \localline
            % Since everything in the group is local, 
            % we have to explicitly make the assignment global
            \global\let\bashline@result\localline
        \egroup
        \closein\bashline@file
        % Clean up after ourselves
        \immediate\write18{rm \bashline@file@name{#1}}
        \bashline@result
    }
    \newcommand{\bashline@command@many}[2][tmp]{%
        \immediate\write18{#2 > \bashline@file@name{#1}}
        \openin\bashline@file=\bashline@file@name{#1}
        % The group localizes the change to \endlinechar
        \newcount\linecnt
        \bgroup
            \endlinechar=-1
            \loop\unless\ifeof\bashline@file 
                \read\bashline@file to \localline%
                \localline
                \newline
            \repeat
        \egroup
        \closein\bashline@file
        % Clean up after ourselves
        \immediate\write18{rm \bashline@file@name{#1}}
    }
    \newcommand{\bashline}[2][tmp]{%
        \bashline@command@one[#1]{#2}%
    }
    \newcommand{\bashlines}[2][tmp]{%
        \bashline@command@many[#1]{#2}%
    }
\makeatother

% Question-answer stuff.

\def\qainpfilename{qa.inp}
\def\qaauxfilename{qa.aux}
\def\qatexfilename{qa.tex}

\newwrite\qafile
\immediate\openout\qafile=\qainpfilename

\newcounter{questionnumber}
\newcounter{questionnumberinchapter}[chapter]

\newcommand{\question}[2]{
    \stepcounter{questionnumber}
    \stepcounter{questionnumberinchapter}

    % Define the placeholder for \answernumberN
    \expandafter\edef\expandafter\answernumber\roman{questionnumber}{PLACEHOLDER:\arabic{questionnumber}}

    DEFINING \texttt{\textbackslash answernumber\roman{questionnumber} as \{PLACEHOLDER:\arabic{questionnumber}\}}

    % Write "x.y #2" to \qainpfilename
    \immediate\write\qafile{\arabic{chapter}.\arabic{questionnumberinchapter} \unexpanded{#2}}

    % Print the qustion to the document.
    Q\arabic{chapter}.\arabic{questionnumberinchapter} #1 \emph{(See answer \expandafter\answernumber\roman{questionnumber}.)}
}

\newcommand{\printtheanswers}{
    \immediate\closeout\qafile
    \bashline{bash bin/generate-answers.sh '\qainpfilename' '\qatexfilename' '\qaauxfilename'}

    \input{\qatexfilename}
}

% Demonstrate that this will not be expanded until after qa.tex has been
% generated loaded into the document.
\def\examplething{this is expanded later}



\begin{document}

TEST

\IfFileExists{\qaauxfilename}{\input{\qaauxfilename}}{AUX DOES NOT EXIST}



\chapter{Maths}

\question{What is $1+2$?}{$1+2=3$ \examplething}

\question{What is $2\times 3$?}{$2\times 3=6$}



\chapter{Science}

\question{Name the six quarks.}{Up down, charm, strange, truth, and beauty.}

\question{How many planets orbit the Sun?}{Eight. Deal with it.}



\chapter{Answers}

\printtheanswers

\end{document}

qa.aux為此文件產生的文件是

\def\answernumberi{3}% (Q1.1): $1+2=3$ \examplething 
\def\answernumberii{1}% (Q1.2): $2\times 3=6$
\def\answernumberiii{4}% (Q2.1): Up down, charm, strange, truth, and beauty.
\def\answernumberiv{2}% (Q2.2): Eight. Deal with it.

我知道您可以使用\providecommand各種 if-then 結構,但是當命令名稱使用\expandafter\answernumber\roman{questionnumber}.如何有條件地定義一個命令,其名稱是由這樣的數字建構的?

答案1

感謝cfr,我現在使用\label\ref

\question命令現在包括

Q\arabic{chapter}.\arabic{questionnumberinchapter} #1 \emph{(See answer \expandafter\ref{\expandafter qanswer\arabic{questionnumber}}.)}

\printtheanswers指令產生如下 tex:

\item\label{qanswer123} (text of answer to the 123rd question)

相關內容