是否可以制作多行列表来对齐其中的文本

是否可以制作多行列表来对齐其中的文本

我正在编写编程 API 的技术规范。我使用 itemize 突出显示方法签名,如下所示:

\begin{itemize}
    \item[\texttt{packeteer.send(message)}] \hfill \\
     is used to send a given \texttt{message} 
     through \texttt{packeteer} object.
\end{itemize}

通常,签名的长度会超过页面宽度,并且会超出边距:

\begin{itemize}
    \item[\texttt{packeteer.send(message, offset, length, port, direction, [function(error, bytes)\{ \}])}] \hfill \\
     is used to send a portion of a given 
     \texttt{message} through
     \texttt{packeteer} object.
\end{itemize}

是否可以让项目内的文本多行并且这些行也能对齐?

就像这样,但是第一行对齐并且它们之间有适当的空格:

\item[{\parbox[t]{1.0\linewidth}{\texttt{packeteer.send(message, offset, length, port, direction, \\ [function(error, bytes)\{ \}])}}}]

答案1

你可以稍微调整一下布局,但是像这样:

在此处输入图片描述

\documentclass{article}
\renewcommand*\descriptionlabel[1]{\hspace\labelsep
 \parbox[t]{\dimexpr\leftmargin+\linewidth-\labelsep}{%
     \normalfont\bfseries\raggedright #1}}

\begin{document}

\begin{description}
    \item[\texttt{packeteer.send(message)}]
     is used to send a given \texttt{message} 
     through \texttt{packeteer} object.
   \item[\texttt{packeteer.send(message, offset, length, port, direction, [function(error, bytes)\{ \}])}] 
     is used to send a portion of a given 
     \texttt{message} through
     \texttt{packeteer} object.
\end{description}
\end{document}

答案2

一种可能性是使用enumitem包裹:

\documentclass{article}
\usepackage{enumitem}

\newcommand\setupdes{%
  \renewcommand\makelabel[1]{%
    \parbox[t]{\dimexpr\leftmargin+\linewidth\relax}{\ttfamily##1}}}
\setlist[description]{labelsep=0pt,before={\setupdes}}

\begin{document}   

\begin{description}
\item[packeteer.send(message)]
     is used to send a given \texttt{message} 
     through \texttt{packeteer} object.
\item[{packeteer.send(message, offset, length, port, direction,[function(error, bytes)\{ \}])}]
     is used to send a portion of a given 
     \texttt{message} through
     \texttt{packeteer} object.
\end{description}

\end{document}

在此处输入图片描述

当然,除了修改之外description,您也可以轻松定义自己的结构。

相关内容