# Printing arguments in \newcommand at different parts of document

I am trying to make a re-useable latex document for setting exam questions together with the answer keys. Because the intended future users will not be familiar with Latex, I have made some templates using \newcommand which can be used by them, and it does all the formatting for them. The new command can be used in this manner.

\questiontext{Enter your question text here}{Enter your answer here}


However, I would just like to make the two arguments in the \newcommand print at different parts of the document but correspond to each other.

The code below shows the MWE of what I have now, and what I would like to have.

\documentclass[a4paper]{article}

\usepackage[english]{babel}
\usepackage{tabularx}
\usepackage{xifthen}% provides \isempty test

\newcounter{questionnumber}[section]
\newcounter{questionpartnumber}[questionnumber]

\newcommand{\questiontext}[2]{
\stepcounter{questionnumber}
\ifthenelse{\isempty{#1}}{\noindent\textbf{\arabic{questionnumber}.}\\}
{\begin{tabularx}{\textwidth}{p{0.5cm} p{12.5cm} @{\extracolsep{\fill}}r}
\textbf{\arabic{questionnumber}.} & \noindent\parbox[t]{\hsize}{#1} & \ifx\empty#2\else[#2]\fi\\\\
\end{tabularx}}
}

\newcommand{\questiontextwoscore}[1]{
\stepcounter{questionnumber}
\ifthenelse{\isempty{#1}}{\noindent\textbf{\arabic{questionnumber}.}\\}
{\begin{tabularx}{\textwidth}{p{0.5cm} p{12.5cm} @{\extracolsep{\fill}}r}
\textbf{\arabic{questionnumber}.} & \noindent\parbox[t]{\hsize}{#1}\\\\
\end{tabularx}}
}

\begin{document}
\section{Questions (Wrong)}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{5}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{500}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{123}

% what i want
\section{Questions (Right)}
\questiontextwoscore{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}
\questiontextwoscore{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}
\questiontextwoscore{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}

\questiontextwoscore{5}
\questiontextwoscore{500}
\questiontextwoscore{123}

\end{document}


My current output is in Section 1, what I am looking for ideally, is in Sections 2 and 3.

To summarize, is there a way to make a latex commands which takes in two arguments, prints all of argument 1 sequentially, then prints all of argument 2 sequentially.

Thanks!

• For a similar question but with different syntax and possibly requirements see tex.stackexchange.com/questions/399536/…
– user30471
Mar 20, 2018 at 7:09
• Thank looks interesting :) Made me think about another perspective. That post used another \newcommand. Maybe I can wrap my custom commands in a newenvironment Mar 20, 2018 at 7:36

Typically, if you need to print something later then you can keep it in macro body (in \answertext macro in my example). This macro keeps the text:

\printQA{1}{answer1}\printQA{2}{answer2} etc.


The \printQA#1#2 macro prints question or answer. It solves the typographical design for such printing and the design is common for questions and answers.

The questions are printed (using\printQA) at the time when \questiontext is processed. The answers are printed when \answertext is processed.

\documentclass{article}

\newcount\QAnum

\long\def\printQA#1#2{\par
\bigskip
}
\let\ea=\expandafter

\long\def\questiontext#1#2{\par
\printQA{\the\QAnum}{#1}
}

\begin{document}
\section{Questions}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{5}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{500}
\questiontext{Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.}{123}


• LaTeX recommends using \newcommand for very good reasons. For instance, your \def\addto would overwrite the macro defined by babel, with possible adverse results. Mar 20, 2018 at 10:40
• @charpi Hello, I can't guess where is your problem without seeing your code. Maybe you need to apply \edef over the part number macro. Maybe you can open new question about it with full example code. Mar 20, 2018 at 19:06