列表以逗号分隔(不是行)

列表以逗号分隔(不是行)

笔记:从逗号分隔列表中删除元素不是重复的。

我想要做的是有一个项目列表环境,比如 itemize 和 enumerate,但我想要输出用逗号而不是新行来分隔。

以这个例子为例http://en.wikibooks.org/wiki/LaTeX/List_Structures

\begin{list_type}

  \item The first item
  \item The second item
  \item The third etc \ldots

\end{list_type}

我想要列表类型输出如下所示的内容:

The first item, The second item, The third etc ...

只有当项目到达页面右边缘时才会换行。因此,如果右边距紧接着“third”,那么我们就会得到:

The first item, The second item,
The third etc ...

代替

The first item, The second item, The third
etc ...

我认为它应该是一个列表结构,这样做是否错误?如何使用 LaTeX 来实现这一点?

答案1

基于 Ulrike 的代码,这里是如何将项目装箱以使它们不会跨越换行符,如问题中所要求的那样:

示例输出

\documentclass{report}

\newlength{\runindent}
\setlength{\runindent}{\parindent}
\newenvironment{runlist}
{\par\renewcommand\item{\ifvmode\hspace{\runindent}\else\unskip,\egroup\ \fi
\hbox\bgroup}\raggedright}
{\egroup\par}

%%%%%%%%%%%%%%
% Following line for demonstration purposes only
\setlength{\textwidth}{6cm}
%%%%%%%%%%%%%%

\begin{document}

Some text for context to fill out space before the list and to show
the line lengths.
\begin{runlist}
  \item The first item
  \item The second item
  \item The third etc \ldots
\end{runlist}
Some text for context to fill out space after the list and to show
the line lengths.

\end{document}

基本思想是将每个项目装箱\hbox。该\hspace命令有两个目的,首先,它强制 latex 进入水平模式,否则第一个项目将被视为一个框,放在自己的行上,其次,它让你通过新的长度控制列表的初始缩进\runindent。(请注意,你不能\parindent直接使用,因为在到达这一点之前,它会在内部设置为零。)最后,通过将项目框设置为 raggedright 来实现正确的换行。

答案2

一个简单的解决方案可以是这样的:

\documentclass{report}
\newenvironment{runlist}
 {\par\renewcommand\item{\unskip\ifvmode\else, \fi}}
 {\par}


\begin{document}
some text
\begin{runlist}
  \item The first item
  \item The second item
  \item The third etc \ldots
\end{runlist}
some text
\end{document}

但细节可能会很棘手。例如,如果您想在某些情况下隐藏逗号,则需要一个可选参数等等。

相关内容