0%

【LaTeX Tips】在文本的同一行开始一个列表环境

有时我们会希望在文本的同一行开始一个列表环境,而非是使用默认的在下一行开始。同时,我们会希望列表环境的标签仍旧在垂直方向上对齐。

LaTeX 提供的 itemize 环境和 enumerate 环境事实上都是 list 环境的一种,但 itemize 和 enumerate 不允许修改 \labelsep 等距离。因此,要达到我们的目的,我们要么使用原始的 list 环境,要么使用 description 环境——它允许修改这些距离。

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum} % just for the example

\newlength{\jeroenlen}
\newenvironment{example}
{\settowidth{\jeroenlen}{\textbf{Example:}}%
\begin{description}[leftmargin=\jeroenlen,labelwidth=0pt,labelsep=0pt]
\item[\textbf{Example:}]%
\begin{itemize}[leftmargin=1.5em,labelsep=.5em]}
{\end{itemize}\end{description}}

\begin{document}
\lipsum[2]
\begin{example}
\item Such and such
\item So and so
\item Enough
\end{example}
\lipsum[3]
\end{document}

效果:


Sometimes, we hope that an itemize environment could start at the same line of certain text, rather than start at the next line as the default does. Moreover, the labels should be vertically aligned.

The itemize and enumerate environment provided by the standard LaTeX are both descendant of list environment, however, length, such as \labelsep, are prevented from modifying. Thus, we have two choices: using list or using description whose length chould be modified.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
\documentclass{article}
\usepackage{enumitem}
\usepackage{lipsum} % just for the example

\newlength{\jeroenlen}
\newenvironment{example}
{\settowidth{\jeroenlen}{\textbf{Example:}}%
\begin{description}[leftmargin=\jeroenlen,labelwidth=0pt,labelsep=0pt]
\item[\textbf{Example:}]%
\begin{itemize}[leftmargin=1.5em,labelsep=.5em]}
{\end{itemize}\end{description}}

\begin{document}
\lipsum[2]
\begin{example}
\item Such and such
\item So and so
\item Enough
\end{example}
\lipsum[3]
\end{document}

And the output:

俗话说,投资效率是最好的投资。 如果您感觉我的文章质量不错,读后收获很大,预计能为您提高 10% 的工作效率,不妨小额捐助我一下,让我有动力继续写出更多好文章。