`siunitx.dtx` 中的 `LaTeXdemo` 环境如何定义?

`siunitx.dtx` 中的 `LaTeXdemo` 环境如何定义?

我想将 翻译siunitx.pdf成另一种语言,所以我必须阅读siunitx.dtx并使用其中的一些源代码。但是.tex当我在源文件中遇到LaTeXdemo环境时,我无法编写自己的文件。 中有一段源代码siunitx.dtx

\lstnewenvironment{LaTeXdemo}[1][code and example]
  {
    \global\let\lst@intname\@empty
    \expandafter\let\expandafter\LaTeXdemo@end
      \csname LaTeXdemo@#1@end\endcsname
    \@nameuse{LaTeXdemo@#1}
  }
  {\LaTeXdemo@end}

我将其复制到我的.tex文件中并写入如下内容:

\documentclass{article}
\usepackage{listings,siunitx}
\makeatletter
\lstnewenvironment{LaTeXdemo}[1][code and example]
  {
    \global\let\lst@intname\@empty
    \expandafter\let\expandafter\LaTeXdemo@end
      \csname LaTeXdemo@#1@end\endcsname
    \@nameuse{LaTeXdemo@#1}
  }
  {\LaTeXdemo@end}
\makeatother
\begin{document}
\begin{LaTeXdemo}
  \num{12345,67890} \\
  \num{1+-2i}       \\
  \num{.3e45}       \\
  \num{1.654 x 2.34 x 3.430}
\end{LaTeXdemo}
\end{document}

但是,我没有得到如所示的预期结果siunitx.pdf。没有相应的输出结果。如何解决这个问题?
在此处输入图片描述

答案1

最新版本的siunitx(v3)即将发布。它有单独的.tex.dtx文件,这意味着代码可能更清晰一些:

\documentclass{article}
\usepackage{listings,siunitx}
\makeatletter
\lst@RequireAspects{writefile}
\newsavebox\LaTeXdemo@box
\lstnewenvironment{LaTeXdemo}[1][code and example]
  {%
    \global\let\lst@intname\@empty
    \edef\LaTeXdemo@end{%
      \expandafter\noexpand\csname LaTeXdemo@@#1@end\endcsname
    }%
    \@nameuse{LaTeXdemo@@#1}%
  }
  {\LaTeXdemo@end}
\newcommand\LaTeXdemo@new[3]{%
  \@namedef{LaTeXdemo@@#1}{#2}%
  \@namedef{LaTeXdemo@@#1@end}{#3}%
}
\newcommand*\LaTeXdemo@common{%
  \setkeys{lst}
    {%
       basicstyle   = \small\ttfamily,
       basewidth    = 0.51em,
       gobble       = 2,
       language     = [LaTeX]{TeX},
    }%
}
\newcount\LaTeXdemo@count
\newcommand*\LaTeXdemo@input{%
  \catcode`\^^M = 10\relax
  \input{\jobname-\number\[email protected]}%
}
\LaTeXdemo@new{code and example}{%
  \setbox\LaTeXdemo@box=\hbox\bgroup
    \global\advance\LaTeXdemo@count by 1 %
    \lst@BeginAlsoWriteFile{\jobname-\number\[email protected]}%
    \LaTeXdemo@common
}{%
    \lst@EndWriteFile
  \egroup
  \begin{center}
    \ifdim\wd\LaTeXdemo@box > 0.48\linewidth
      \begin{minipage}{\linewidth}
        \usebox\LaTeXdemo@box
      \end{minipage}%
      \par
      \begin{minipage}{\linewidth}
        \LaTeXdemo@input
      \end{minipage}
    \else
      \begin{minipage}{0.48\linewidth}
        \LaTeXdemo@input
      \end{minipage}%
      \hfil
      \begin{minipage}{0.48\linewidth}
        \usebox\LaTeXdemo@box
      \end{minipage}%
    \fi
  \end{center}
}
\LaTeXdemo@new{code and float}{%
  \global\advance\LaTeXdemo@count by 1 %
  \lst@BeginAlsoWriteFile{\jobname-\number\[email protected]}%
  \LaTeXdemo@common
}{%
  \lst@EndWriteFile
  \LaTeXdemo@input
}
\LaTeXdemo@new{code only}{\LaTeXdemo@common}{}
\makeatother

\begin{document}
\begin{LaTeXdemo}
  \num{12345,67890}  \\
  \num{.3e45}        \\
\end{LaTeXdemo}
\end{document}

您将看到一些通用代码,用于逐字收集演示,然后选择如何处理它。在您的尝试中,您没有通用代码,只有执行演示和代码的部分。

相关内容