我的论文包含许多description
环境。在向我的导师展示后,他抱怨项目标题和描述之间缺少分隔。我尝试重新定义命令,以便一次性\item
更改所有环境,而无需手动更改每个环境。description
\item
虽然我的方法适用于简单的项目(第一个),但它对于描述中包含多段文本的项目(如第二个)却失败了。
\documentclass{book}
\let\olddescription\description
\def\description{%
\olddescription \let\olditem\item%
\def\item[##1]##2{\olditem[##1]{\hfill \\##2}}}
\begin{document}
\begin{description}
\item[The First Item]{This text describes the first item.}
\item[The Second Item]{This text describes the second item.
Contrary to the description of the first item, it is quite a bit longer.
Additionally, it consists of multiple paragraphs, which causes problems
with redefining the item command.}
\end{description}
\end{document}
使用 pdflatex 进行编译时,出现以下错误消息:
Runaway argument?
{This text describes the second item. Contrary to the description of \ETC.
! Paragraph ended before \item was complete.
<to be read again>
\par
l.14
?
! Extra }, or forgotten \endgroup.
l.15 ...roblems with redefining the item command.}
?
如果我删除第二项,它就会起作用。有没有办法修复我的\item
命令?
我知道我可以descriptionlabel
像这样重新定义命令:
\let\olddescriptionlabel\descriptionlabel
\renewcommand*\descriptionlabel[1]{\olddescriptionlabel{#1:}}
但不幸的是,这对我尝试使用我在以下位置找到的代码在标题后添加的换行符不起作用http://en.wikibooks.org/wiki/LaTeX/List_Structures#Description。
答案1
在他的回答,埃格尔已经解释了您当前定义的问题以及修改方法。但是,我建议您使用包提供的nextline
样式:desciption
enumitem
\documentclass{book}
\usepackage{enumitem}
\setlist[description]{style=nextline}
\begin{document}
\begin{description}
\item[The First Item]
This text describes the first item.
\item[The Second Item]
This text describes the second item.
Contrary to the description of the first item, it is quite a bit longer.
Additionally, it consists of multiple paragraphs, and doesn't cause problems
since we didn't redefine the item command.
\end{description}
\end{document}
答案2
您的方法是错误的,您\item
用两个参数重新定义;原来\item
只有一个(至少对于description
):
\documentclass{book}
\expandafter\def\expandafter\description\expandafter{%
\description \let\olditem\item
\def\item[##1]{\olditem[##1]\mbox{}\\}}
\begin{document}
\begin{description}
\item[The First Item] This text describes the first item.
\item[The Second Item]This text describes the second item.
Contrary to the description of the first item, it is quite a bit longer.
Additionally, it consists of multiple paragraphs, which causes problems
with redefining the item command.
\end{description}
\end{document}
代码中已有的括号不会产生任何作用,因此没有必要将其删除。
\long\def\item[##1]##2{...}
如果您这样说以允许\par
第二个参数中的标记,您的代码就会起作用。
当然,采用这种方法enumitem
更好。