我正在用 LaTeX 写一份问卷,想做一个问题的宏。我想要的一个功能是使用标签交叉引用问题(例如,以防您想跳过某个问题)。以下是我目前拥有的 MWE:
\documentclass{article}
\usepackage{tabu,textcomp,amsmath,longtable}
\usepackage[left=2cm, right=2cm, top=2cm]{geometry}
\newcounter{qnumber}
\setcounter{qnumber}{0}
\stepcounter{qnumber}
\newcommand{\question}[3]{
\hfill \theqnumber\hfill\hfill & #1 & #2 & #3\stepcounter{qnumber}\\ \hline
}
\begin{document}
\begin{longtabu} to \textwidth {|c|X|X|X|}
\hline
\textbf{Sl.} &
\textbf{Question text} &
\textbf{Instructions for enumerator (not to be read out)} &
\textbf{Response options}
\\ \hline
\question{What is your age?}{Enter age in years}{18--99}
\question{Do you rent your house?}{}{1. Yes; 2. No}
\end{longtabu}
\end{document}
我希望有另一组花括号,\question
可以为问题提供标签,然后允许交叉引用问题编号。例如:
\question{Do you rent your house}{lab:rent}
\question{If you answered "Yes" in question \ref{lab:rent}, how much do you pay per month}
我该如何调整这条\newcommand
线路才能让它工作呢?
答案1
例如,您可以使用一个可选参数,\question
如果不为空,则该参数将成为标签:
\documentclass{article}
\usepackage{tabu,textcomp,amsmath,longtable}
\usepackage[left=2cm, right=2cm, top=2cm]{geometry}
\newcounter{qnumber}
\setcounter{qnumber}{0}
\newcommand{\question}[4][]{%
\refstepcounter{qnumber}%
\if\relax\detokenize{#1}\relax
\else
\label{#1}%
\fi
\hfill\theqnumber\hfill\hfill & #2 & #3 & #4\\ \hline
}
\begin{document}
\begin{longtabu} to \textwidth {|c|X|X|X|}
\hline
\textbf{Sl.} &
\textbf{Question text} &
\textbf{Instructions for enumerator (not to be read out)} &
\textbf{Response options}
\\ \hline
\question{What is your age?}{Enter age in years}{18--99}
\question[lab:rent]{Do you rent your house?}{}{1. Yes; 2. No}
\question{If you answered "Yes" in question \ref{lab:rent}, how much do you pay per month}{}{}
\end{longtabu}
\end{document}