我正在尝试将单个描述项的行对齐。当我引入换行符或行变得太长时,新行不会与第一行对齐。我知道第一行必须处理标签占用的空间,但我希望其他行无论如何都从相同的位置(水平)开始。以下是说明我的问题的示例代码:
\documentclass[12pt,a4paper]{article}
\begin{document}
\begin{description}
\item [Label]Here is a long item that requires a line break. The second line of this item
won't line up with the first word. I can achieve this manually\\ \hspace*{2mm} by forcing a
line break and setting an apropriate value in \textbackslash hspace*, but I want this to be
automised. Please help me.
\end{description}
\end{document}
结果如下:
我很感激任何帮助。这真让我抓狂。
答案1
如果我理解正确的话,labeling
KOMA-script 包提供的环境可以完成您想要的工作:
\documentclass[12pt,a4paper]{article}
\usepackage{scrextend}
\usepackage{blindtext}
\setkomafont{labelinglabel}{\bfseries}
\usepackage{showframe}
\begin{document}
\begin{labeling}{A long long label}%Giving the longest label for alignment
\item [Label]
Here is a long item that requires a line break. The second
line of this item won't line up with the first word. I can
achieve this manually\\
by forcing a line break and setting an apropriate value in
\textbackslash hspace*, but I want this to be automised.
Please help me.
\item [A long long label]
a manual linebreak, though not
recommended \\
\blindtext
\end{labeling}
\end{document}
这里包scrextend
提供了您所需的一切,切换到 koma 类(scrartcl
)可能是一个优势。labeling
环境以最长的标签作为参数,需要正确对齐。这个参数也可以更短,只需玩一下。
答案2
作为 KOMA 类或的替代scrextend
,您可以使用enumitem
包,将键设置labelwidth
为标签的宽度。
我通过结合(a)得到了这个解决方案伯纳德的回答到使用描述环境的简单年表以及(b)伯纳德的回答到如何将描述的左边距设置为 enumitem 中特定标签的宽度?您可以在以下位置找到相同的解决方案Gonzalo Medina 的回答到具有对齐描述的描述列表。
\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\begin{document}
\begin{description}[%
labelwidth=3cm,%
leftmargin=!%
]
\item [Label]Here is a long item that requires a line break. The second line of this item
won't line up with the first word. I no longer need to achieve this manually by forcing a
line break and setting an appropriate value in \textbackslash hspace*.
Here's a second paragraph of the same item.
\item [A longer label]Here is a second item. It has a longer label, necessitating a larger labelwidth.
\end{description}
\end{document}
输出如下:
calc
您还可以通过添加包并使用其命令来计算最宽标签的宽度\widthof
:
\documentclass[12pt,a4paper]{article}
\usepackage{enumitem}
\usepackage{calc}% Supports \widthof command
\begin{document}
\begin{description}[%
labelwidth=\widthof{\bfseries A longer label},%
leftmargin=!%
]
\item [Label]Here is a long item that requires a line break. The second line of this item
won't line up with the first word. I no longer need to achieve this manually by forcing a
line break and setting an appropriate value in \textbackslash hspace*.
Here's a second paragraph of the same item.
\item [A longer label]Here is a second item. It has a longer label, necessitating a larger labelwidth.
\end{description}
\end{document}
这基本上给出了相同的输出。请注意\widthof{\bfseries A longer label}
,在中必须指定\bfseries
:标签排版为粗体。您需要确保您测量宽度的文本\widthof
也排版为粗体。
您可以通过让环境自动确定哪个标签最宽,进一步实现自动化。请参阅Gonzalo Medina 的回答到labelwidth
根据最宽的标签自动设置描述列表?。