我正在尝试设计一个由一系列问题和答案组成的基本“访谈格式”,我想用不同的样式来排版,例如,以“Q:”开头的问题用粗体表示,以“A:”开头的答案用普通文本表示
到目前为止,我已经有了以下代码,可以生成我想要的结果,但是有点麻烦:
\begin{list}{}{}
\item[\textbf{Q:}] \textbf{This is the first question}
\item[A:] And this is the answer
\item[\textbf{Q:}] \textbf{This is the second question}
\item[A:] Another answer
\end{list}
结果是:
我甚至设法定义了一个新的环境和几个命令来节省输入时间:
\newenvironment{interview}
{\begin{list}{}{}}
{\end{list}}
\newcommand{\question}[1]{\item[\textbf{Q:}] \textbf{#1}}
\newcommand{\answer}[1]{\item[A:] #1}
\begin{interview}
\question {This is the first question}
\answer {And this is the answer}
\question {This is the second question}
\answer {Another answer}
\end{interview}
这让我很满意,但我想知道是否可以用其他方式来定义,以避免将每个“项目”的参数括在花括号之间,就像我们在普通的“项目化”列表中所做的那样。换句话说:是否有可能以如下方式定义事物?:
\begin{interview}
\question This is the first question
\answer And this is the answer
\question This is the second question
\answer Another answer
\end{interview}
答案1
开始\bfseries
提出问题,恢复\normalfont
以获得答案。
\documentclass{article}
\newenvironment{interview}
{\begin{list}{}{}}
{\end{list}}
\newcommand{\question}{\bfseries\item[Q:]}
\newcommand{\answer}{\normalfont\item[A:]}
\begin{document}
\begin{interview}
\question This is the first question
\answer And this is the answer
\question This is the second question
\answer Another answer
\end{interview}
\end{document}
项目之间的空行是个人喜好;我相信它们比缩进代码更好。
答案2
我会用枚举项包来定义一个新的列表样式,这样你就可以输入
\begin{interview}
\item This is the first question
\item And this is the answer
\item This is the second question
\item Another answer
\end{interview}
生产
你需要做的主要事情是改变\item
命令的默认行为,以便它在你的问:和A:标记。以下是执行此操作的代码:
\documentclass{article}
\usepackage{enumitem}
\let\realitem\item% save the "real" \item command
\newcommand\questionansweritem{%
\refstepcounter{enumi}
\realitem[\ifodd\theenumi\textbf{Q:}\else\textbf{A:}\fi]
}
\newlist{interview}{enumerate}{1}
% make the interview list use \questionansweritem instead of \item
\setlist[interview]{before=\let\item\questionansweritem}
\begin{document}
\begin{interview}
\item This is the first question
\item And this is the answer
\item This is the second question
\item Another answer
\end{interview}
\end{document}
如果您更喜欢使用\question
和\answer
,这是一个好主意,因为这会使代码更具可读性,您可以通过添加以下行来实现:
\let\answer\questionansweritem
\let\question\questionansweritem
使用的主要优点是枚举项包的优点在于,它可以轻松更改面试问题列表的间距等——enumitem
有关更多详细信息,请参阅文档。
您可以轻松自定义列表,例如,将问题以斜体显示:
这是通过使用以下方法实现的:
\documentclass{article}
\usepackage{enumitem}
\let\realitem\item% save the "real" \item command
\newcommand\questionansweritem{%
\refstepcounter{enumi}
% \item + turn on italics
\ifodd\theenumi\realitem[\textbf{Q:}]\bgroup\itshape
% \turn off italics + \item
\else\egroup\realitem[\textbf{A:}]\fi
}
\newlist{interview}{enumerate}{1}
\setlist[interview]{before=\let\item\questionansweritem}
\let\answer\questionansweritem
\let\question\questionansweritem
\begin{document}
\begin{interview}
\question This is the first question
\answer And this is the answer
\question This is the second question
\answer Another answer
\end{interview}
\end{document}