# How does the LaTeXdemo environment in siunitx.dtx be defined?

I want to translate the siunitx.pdf to another language, so I have to read the siunitx.dtx and make use of some source code in it. But I have problem to write my own .tex file when I meet the LaTeXdemo environment in the source file. There is a source code segment in 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}


I copy it to my .tex file and write as follows:

\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}


However, I don't get the expected result as shown in siunitx.pdf. There is not the corresponding output result. How to solve this problem?

• The full definition of \LaTeXdemo is larger, including all the helper macros and definitions it runs from line 191 to line 366 in the current siunitx.dtx. May 7 at 15:09
• I add all of the segment as you said to my .tex file but I get the error : ! Undefined control sequence. \LaTeXdemo@input->\MakePercentComment..... May 7 at 15:24
• Of course there could be other dependencies. You could try adding everything, but probably it is the easiest to copy the full .dtx, make translations in that file, and run LaTeX on the .dtx (see tex.stackexchange.com/questions/212776/…). May 7 at 15:31

The latest version of siunitx (v3) will be released soon. It has separate .tex and .dtx files, meaning that the code is perhaps a little clearer:

\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\LaTeXdemo@count.tmp}%
}
\LaTeXdemo@new{code and example}{%
\setbox\LaTeXdemo@box=\hbox\bgroup
\global\advance\LaTeXdemo@count by 1 %
\lst@BeginAlsoWriteFile{\jobname-\number\LaTeXdemo@count.tmp}%
\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\LaTeXdemo@count.tmp}%
\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}
`

You'll see there is some common code, used to collect up the demo verbatim, then a choice of what to do with it. In your attempt, you don't have the common code, only the part that does a demo and code next to each other.

• Most of the manual is the same text between v2 and v3, although the keys are reworked and a few interfaces are altered. May 7 at 16:26
• That really works! Thanks a lot! May 8 at 5:35