如何定义自定义描述环境?

如何定义自定义描述环境?

我想定义一个新的“自定义description”环境,假设my description采用一个长度参数并产生通过以下代码实现的结果:

\documentclass{article}

\usepackage{enumitem}
\usepackage{calc}

\begin{document}
    
    Here's some text before the list.
    
    \begin{description}[align=right, itemindent=0cm, leftmargin=\labelsep+3cm, font=\ttfamily]
        \item [The first item] Math is so good!
        \item [Here's the second one!] Let's do some math!
    \end{description}

    And here's something for after the list.
    
\end{document}

这里,leftmargin=3cm+\labelsep3cm我想要传递“长度”参数的地方。

上述代码的输出如下所示:

在此处输入图片描述

另一件事:如果我还可以传递另一个参数来控制、等font之间的值,那就非常方便了。\ttfamilybfseries

答案1

只是

% definition
\NewDocumentEnvironment{my desp}{ m m }{%
  \begin{description}[align=right, itemindent=0cm, leftmargin=\labelsep+#1, font=#2]
}{
  \end{description}%
}

% usage
\begin{my desp}{3cm}{\ttfamily}
  \item ...
  \item ...
\end{my desp}

完整示例

\documentclass{article}
\usepackage{enumitem}
\usepackage{calc}

\NewDocumentEnvironment{my desp}{ m m }{%
  \begin{description}[align=right, itemindent=0cm, leftmargin=\labelsep+#1, font=#2]
}{
  \end{description}%
}

\begin{document}
    
    Here's some text before the list.
    
    \begin{description}[align=right, itemindent=0cm, leftmargin=\labelsep+3cm, font=\ttfamily]
        \item [The first item] Math is so good!
        \item [Here's the second one!] Let's do some math!
    \end{description}

    And here's something for after the list.
    
    \begin{my desp}{3cm}{\ttfamily}
        \item [The first item] Math is so good!
        \item [Here's the second one!] Let's do some math!
    \end{my desp}
\end{document}

在此处输入图片描述

相关内容