我希望下面代码的输出像这张图片一样。怎么做?
请记住,[Foo Bar Foo] 的宽度是可变的。所以它应该自动计算。
\documentclass{book}
\begin{document}
\noindent Just enough text to make the line break so we get to see a second line.
\begin{itemize}
\item[Foo] An item
\item[Foo Bar Foo] An item An item An item An item An item An item An item An item An item An item An item An item An item An item
\item[Bar Bar] An item
\end{itemize}
Just enough text to make the line break so we get to see a second line.
\end{document}
编辑:我不想使用表格或类似的环境,因为有许多项目应该跨越一些页面。
答案1
如果的主体中没有标签或计数器设置(例如编号方程式)等危险的东西itemize
,您可以本地重新定义\item
以进行测量:
\documentclass{article}
\usepackage{enumitem,environ}
\newlength{\xitemlenx}
\newlength{\xitemleny}
\NewEnviron{xitemize}{%
\setbox0=\vbox{
\xitemlenx=0pt
\def\item[##1]{\measureitem{##1}}
\BODY
\global\xitemleny\xitemlenx
}%
\itemize[
leftmargin=\dimexpr\xitemleny+\labelsep,
labelwidth=\xitemleny,
align=left
]
\BODY
\enditemize
}
\newcommand{\measureitem}[1]{%
\settowidth{\dimen0}{#1}%
\ifdim\dimen0>\xitemlenx
\xitemlenx=\dimen0
\fi
}
\begin{document}
Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
\begin{xitemize}
\item[Foo] An item
\item[Foo Bar Foo] An item An item An item An item An item An item An item An item An item An item An item An item An item An item
\item[Bar Bar] An item
\end{xitemize}
Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
\end{document}
如果您需要单个项目,正如您从评论中看到的那样,这会更容易,因为只需要测量一个标签;这是集成在前面的代码中的想法。
\documentclass{article}
\usepackage{enumitem,environ}
\newlength{\xitemlenx}
\newlength{\xitemleny}
\NewEnviron{xitemize}{%
\setbox0=\vbox{
\xitemlenx=0pt
\def\item[##1]{\measureitem{##1}}
\BODY
\global\xitemleny\xitemlenx
}%
\itemize[
leftmargin=\dimexpr\xitemleny+\labelsep,
labelwidth=\xitemleny,
align=left
]
\BODY
\enditemize
}
\newcommand{\measureitem}[1]{%
\settowidth{\dimen0}{#1}%
\ifdim\dimen0>\xitemlenx
\xitemlenx=\dimen0
\fi
}
\newcommand{\singleitem}[2]{%
\settowidth{\xitemleny}{#1}%
\begin{itemize}[
leftmargin=\dimexpr\xitemleny+\labelsep,
labelwidth=\xitemleny,
align=left
]
\item[#1]\textbf{#2}
\end{itemize}%
}
\begin{document}
Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
\begin{xitemize}
\item[Foo] An item
\item[Foo Bar Foo] An item An item An item An item An item An item An item An item An item An item An item An item An item An item
\item[Bar Bar] An item
\end{xitemize}
Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
Now a single item:
\singleitem{Foo Bar}{An item An item An item An item An item An item An item An item An item An item An item An item An item An item}
Just enough text to make the line break so we get to see a second line.
Just enough text to make the line break so we get to see a second line.
\end{document}
答案2
使用结合了和ltablex
功能的软件包,一切都变得非常容易:tabularx
longtable
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{fourier, heuristica}
\usepackage{array, ltablex}
\begin{document}
\noindent Just enough text to make the line break so we get to see a second line. Text text text text tex text text text tex.
\begin{tabularx}{\linewidth}{@{}l@{\quad}X@{}}
Foo & An item \\
Foo Bar Foo & An item An item An item An item An item An item An item An item An item An item An item An item An item An item\\
Bar Bar & An item
\end{tabularx}
Just enough text to make the line break so we get to see a second line. Text text text text tex text text text tex.
\end{document}