如何编写在环境中表现得像 \item 的宏?

如何编写在环境中表现得像 \item 的宏?

我正在尝试向项目添加以下形式的环境:

\begin{example}
  \source some text goes here to be used as an example.
  \target some more text goes here as a translation for the first line.
  \expl   here goes some text that explains the translation or provides
          some comment. This part tends to be longer and may span several
          lines, but never be more than a single paragraph.
\end{example}

我想重现的是\item宏在itemize环境中的行为;但是,我不知道从哪里开始。只要可能,我更希望有一个不需要括号但采用以下行(包括单行换行符)作为参数分隔符的宏。

写的内容如下:

\def\source#1\par{%
  \textit{#1}\par}

正确的做法是什么?

答案1

这似乎是一份工作\list=)

\newenvironment{example}{
  \newcommand{\source}{\item\itshape}
  \newcommand{\target}{\item\upshape\bfseries}
  \newcommand{\expl}{\item\normalfont}
  \list{}{}}
{\endlist}

\list然后,您可以更改定义内的第二个参数,example通过更改适当的长度来设置间距(例如\itemsep)。


相反,如果你想知道如何让\source宏将以下段落作为其参数,那么\item据我所知,这不是可以做到的。但是,你可以使用某种分隔符来实现这一点,正如你已经建议的那样,\par在你的 中使用\def。在这种情况下,你需要用空行分隔每个项目,以便插入\par并且你的宏可以找到其参数的结尾。如果你需要能够吞下多个段落,这显然会失败。

\documentclass{article}

\newenvironment{example}{
  \def\source##1\par{%
    \textit{##1}\par}
  \def\target##1\par{\textbf{##1}\par}
  \def\expl##1\par{##1\par}
}{}


\begin{document}

\begin{example}
  \source some text goes here to be used as an example.

  \target some more text goes here as a translation for the first line.

  \expl   here goes some text that explains the translation or provides
          some comment. This part tends to be longer and may span several
          lines, but never be more than a single paragraph.

\end{example}

\end{document}

更新:比使用 \par 作为分隔符更好的解决方案

用作\par分隔符有许多缺点。我们可以通过使用\source \target\expl本身作为分隔符来避免多余的行等:

\documentclass{article}

\long\def\example\source#1\target#2\expl#3\endexample{\textit{#1}\par\textbf{#2}\par#3\par}

\begin{document}

\example
  \source some text goes here to be used as an example.
  \target some more text goes here as a translation for the first line.
  \expl   here goes some text that explains the translation or provides
          some comment. This part tends to be longer and may span several
          lines, but never be more than a single paragraph.
\endexample

\end{document}

您不能example再将其用作环境,但这为您提供了几乎相同的语法和对每个部分的完全控制。此解决方案要求所有三个部分始终存在,即使为空,并且按特定顺序存在。使它们可选是可能的,但很麻烦。


获得所需内容的第三种方法

我认为您的最终目标是拥有可读的源代码。在这种情况下,您可以考虑使用 key-val 接口。例如,您可以使用包pgfkeys并执行

\documentclass{article}

\usepackage{pgfkeys}

\pgfkeys{example/.is family, example/.cd,
  source/.code = {\item \textit{#1}},
  target/.code = {\item \textbf{#1}},
  expl/.code = {\item #1}
}

\def\example#1{
  \begin{list}{}{}
    \pgfkeys{example/.cd, #1}
  \end{list}
}

\begin{document}

\example{%
  source = {some text goes here to be used as an example.},
  target = {some more text goes here as a translation for the first line.},
  expl   = {here goes some text that explains the translation or provides
            some comment. This part tends to be longer and may span several
            lines, but never be more than a single paragraph.}
}

\end{document}

答案2

标准description环境能帮到你吗?可以避免使用括号newcommand来调用它:

\documentclass{article}

\newcommand{\startexample}{\begin{description}}
\newcommand{\stopexample}{\end{description}}
\def\source#1{\item[source:]#1}
\def\target#1{\item[target:]#1}
\def\expl#1{\item[explanation:]#1}

\begin{document}

\startexample
  \source some text goes here to be used as an example.
  \target some more text goes here as a translation for the first line.
  \expl here goes some text that explains the translation or provides
        some comment. This part tends to be longer and may span several
        lines, but never be more than a single paragraph.
\stopexample

\end{document}

相关内容