pgf:带有逐字代码的代码示例

pgf:带有逐字代码的代码示例

我使用 pgfmanual 来记录库和美观的codeexample环境来排版代码块 + 结果。但是,当代码包含逐字代码时,我想使用它。

是否可以?

MWE(请注意,我使用这里是+v为了简单起见,实际上我主要使用该xsim包来读取参数并将它们写入文件......但希望它不会有太大不同)失败:

ERROR: LaTeX cmd Error: Verbatim-like command '\testVerbatim' illegal in argument.
\documentclass[a4paper,doc2]{ltxdoc}

\usepackage{amsmath}
\usepackage[margin=3cm]{geometry}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{shadows,fit}
% Loads the great package that produces tikz-like manual (see also tikzcd for examples)
\input{pgfmanual-en-macros.tex} % Is supposed to be included in recent TeX distributions, but I get errors...
\usepackage{verbatim}

\NewDocumentCommand{\testVerbatim}{+v}{
\begin{flushleft}\ttfamily%
#1
\end{flushleft}}

\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\begin{document}

\begin{codeexample}[vbox]
  This works fine: $\delta$.
\end{codeexample}

\testVerbatim{I can put # or even % in this sentence}

\begin{codeexample}[vbox]
  \testVerbatim{I can put # or even % in this sentence}
\end{codeexample}
  
\end{document}

答案1

我终于找到了一个解决方案(编辑:请参阅下文以获得更强大的解决方案),使用xsim将内容写入文件,然后我使用了密钥from file

\documentclass[a4paper,doc2]{ltxdoc}

\usepackage{amsmath}
\usepackage[margin=3cm]{geometry}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{shadows,fit}
% Loads the great package that produces tikz-like manual (see also tikzcd for examples)
\input{pgfmanual-en-macros.tex} % Is supposed to be included in recent TeX distributions, but I get errors...
\usepackage{verbatim}
\RequirePackage{xsimverb} % To easily write verbatim code to files
\NewDocumentEnvironment{codeAndResult}{}{\XSIMfilewritestart{\jobname-robExt-tmp-file-you-can-remove.tmp}}%
{%
  \XSIMfilewritestop%
  \codeexample[leave comments, vbox, from file={\jobname-robExt-tmp-file-you-can-remove.tmp}]%
}


\NewDocumentCommand{\testVerbatim}{+v}{
\begin{flushleft}\ttfamily%
#1
\end{flushleft}}

\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\begin{document}

\begin{codeexample}[vbox]
  This works fine: $\delta$.
\end{codeexample}

\testVerbatim{I can put # or even % in this sentence}

\begin{codeAndResult}
  \testVerbatim{I can put # or even % in this sentence}
\end{codeAndResult}
  
\end{document}

编辑

我发现这个版本更可靠,因为有时以前的解决方案不会打印任何内容:

\documentclass[a4paper,doc2]{ltxdoc}

\usepackage{amsmath}
\usepackage[margin=3cm]{geometry}
\usepackage{calc}
\usepackage{tikz}
\usetikzlibrary{shadows,fit}
% Loads the great package that produces tikz-like manual (see also tikzcd for examples)
\input{pgfmanual-en-macros.tex} % Is supposed to be included in recent TeX distributions, but I get errors...
\usepackage{verbatim}
\RequirePackage{xsimverb} % To easily write verbatim code to files
\NewDocumentEnvironment{codeAndResult}{}{\XSIMfilewritestart{\jobname-codeAndResult-tmp-file-you-can-remove.tmp}}%
{%
  \XSIMfilewritestop%
  \par
  \medskip
  \noindent\colorbox{graphicbackground}{\noindent\begin{minipage}{1.0\linewidth}
  {\input{\jobname-codeAndResult-tmp-file-you-can-remove.tmp}}
  \end{minipage}}
  % I don't know why, but sometimes this prints nothing, while input works:
  %{\codeexample[width=0pt, leave comments, vbox, from file={\jobname-codeAndResult-tmp-file-you-can-remove.tmp}]}%
  {\codeexample[code only, from file={\jobname-codeAndResult-tmp-file-you-can-remove.tmp}]}%
}


\NewDocumentCommand{\testVerbatim}{+v}{
\begin{flushleft}\ttfamily%
#1
\end{flushleft}}

\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}


\begin{document}

\begin{codeexample}[vbox]
  This works fine: $\delta$.
\end{codeexample}

\testVerbatim{I can put # or even % in this sentence}

\begin{codeAndResult}
  \testVerbatim{I can put # or even % in this sentence}
\end{codeAndResult}
  
\end{document}

相关内容