宏缩进甚至在换行符上

宏缩进甚至在换行符上

我是第一次创建自己的命令(这是我的第一个命令),但似乎无法实现我想要做的事情。

我当前的命令如下所示

\documentclass{article}
\usepackage{xparse}
\DeclareDocumentCommand \acro { m m o } {%
    \noindent\textbf{#1}\hspace{1em}#2 \IfNoValueF {#3} {\\
        \noindent\phantom{#1}\hspace{1em}#3
    }\vspace{1em}
}

\begin{document}

\acro{TCP}{Transmission Control Protocol}[This is an explanation that can be pretty long and should be indented the same even if it extends into a new line.]

\end{document}

它需要两个强制参数和一个可选参数。我的问题在于可选参数。该命令用于创建一个简单的词汇表(我查看了默认词汇表,但它并没有达到我想要的效果)。第一个参数是首字母缩略词(如 TCP),第二个参数是首字母缩略词的含义(传输控制协议),第三个参数是对其实际含义的详细解释。

现在我想要的结果是这样的

TCP    Transmission Control Protocol
       This is an explanation that can be pretty long and should be indented
       the same even if it extends into a new line.

但是我好像没法控制第二行的缩进,结果如下

TCP    Transmission Control Protocol
       This is an explanation that can be pretty long and should be indented
the same even if it extends into a new line.

我试过 parbox、makebox、minipage 和一些我现在想不起来了的。这些方法的问题是,我必须指定框的宽度,但我不知道框应该有多宽,因为缩写词(或者说应该解释的单词)可能很长,也可能很短。

任何帮助将不胜感激。

[解决方案]

\documentclass{article}
\usepackage{xparse}
\usepackage{enumitem}
\newlist{acrolist}{description}{1}
\setlist[acrolist{font=\bfseries,leftmargin=4em,labelwidth=4em,labelsep=0pt}
\NewDocumentCommand\acro{mmo}{%
    \item[#1] #2 \IfValueT{#3}{\\
                #3
    }
    \vspace{0.5em}
}
\begin{document}
\begin{acrolist}
    \acro{TCP}{Transmission Control Protocol}[This is an explanation that can be pretty long and should be indented the same even if it extends into a new line.]
    \acro{ARP}{Address Resolution Protocol}
    \acro{PHY}{Physical}[The physical (MAC) address of a device]
\end{acrolist}

\end{document}

谢谢曼努埃尔感谢所有建议和提供的答案。

答案1

你也可以使用列表(这是我的做法)。加载enumitem

\begin{description}[font=\normalfont,leftmargin=5em,labelwidth=5em,labelsep=0pt]
  \item[TCP] Transmission Control Protocol \\
             This is an explanation that can be pretty long and should be
             indented the same even if it extends into a new line.
\end{description}

如果有界面就好了?那么

\usepackage{enumitem}
\newlist{acrolist}{description}{1}
\setlist[acrolist]{font=\bfseries,leftmargin=4em,labelwidth=4em,labelsep=0pt}
\NewDocumentCommand\acro{mmo}{\item[#1] #2 \IfValueT{#3}{\\ #3}}

然后使用

\begin{acrolist}
  \acro{one}{some text}[explanation]
  \acro{two}{some other text}[explanation]
  \acro{TCP}{Transmission Control Protocol}[This is an explanation that can be
             pretty long and should be indented the same even if it extends into
             a new line.]
\end{acrolist}

或者

\begin{tabular}{lp{8cm}}
  TCP & Transmission Control Protocol \\
      & This is an explanation that can be pretty long and should be indented
        the same even if it extends into a new line.
\end{tabular}

或者,加载tabularx包并使用

\begin{tabularx}{\textwidth}{lX}
  TCP & Transmission Control Protocol \\
      & This is an explanation that can be pretty long and should be indented the
        same even if it extends into a new line.
\end{tabularx}

确实解决了您的问题(我认为)。当然,您可以定义您\acro希望以这种方式工作。

答案2

与描述列表一样变体。leftmargin 是可选参数(默认为 4em):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{xparse}

\usepackage{enumitem}

\newlist{acroglossary}{description}{1}
\setlist[acroglossary]{leftmargin = 4em, labelwidth =0pt, labelsep=!}%

\DeclareDocumentCommand \acro { m m o } {%
    \item[\rlap{#1}]\textit{#2} \IfNoValueF {#3} {\smallskip\\#3 }
    }%

\begin{document}

\begin{acroglossary}
\item[\rlap{TCP}] \textit{Transmission Control Protocol}\\[1ex]
This is an explanation that can be pretty long and should be indented the same even if it extends into a new line.
\end{acroglossary}

\begin{acroglossary}[leftmargin =6em]
\acro{FTGFOP}{Finest Tippy Golden Flowery Orange Pekoe}%
[Highest quality whole leaf grade for tea. \\ Also, as a joke, ‘Far Too Good For Ordinary People’.]
\end{acroglossary}

\end{document} 

在此处输入图片描述

相关内容