我的尝试

我的尝试

更新:我已经提出了一个后续问题:如何排版环境及其在环境中的文字等价物?


我想将命令和参数传递给环境。输出应为三重:

  1. 文字命令
  2. 排版版本(使用时应如何显示)
  3. 命令的定义。(这只是命令用途的文字描述。)

我的尝试

\key对于此示例,我尝试使用menukeys包裹

\documentclass{article}
\usepackage{fontspec}
\usepackage{environ}
\usepackage{marginnote}
\usepackage{menukeys}
\NewEnviron{command}[1]{% 
\par
\reversemarginpar\marginnote{\texttt{\string#1}}
\BODY
\par
\textbf{Example:} % And here is #1 with #2, but literally typeset (e.g. literally \keys{Shift + F5})
\par
%#1#2 % <-- I'd like to typeset these two inputs properly (e.g. non-literal \keys{Shift + F5})
\par
}%


\begin{document}

\begin{command}{\keys}% {{Shift + F5}} % Example input does not work
This command allows you to add keyboard strokes. % here is the definition followed by an example on the next line.
\end{command}

\end{document}

期望输出

在此处输入图片描述

我的想法

也许这甚至不是正确的方法。我不知道如何有效地处理带有多个参数的命令(例如使用包创建的命令xparse)。

答案1

您应该逐字逐句地吸收这两个论点,这样xparse就可以了;然后,当您想要显示效果时,您可以“重新扫描”这两个论点。

\documentclass{article}
\usepackage{fontspec}
\usepackage{menukeys}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentEnvironment{command}{vv}
 {
  \tl_set:Nn \l_macmad_argument_i_tl { #1 }
  \tl_set:Nn \l_macmad_argument_ii_tl { #2 }
  \par
  \noindent
  \makebox[0pt][r]{\ttfamily\l_macmad_argument_i_tl\hspace{2em}}
  \ignorespaces
 }
 {
  \par\nopagebreak
  \noindent
  \textbf{Example:~}
  \texttt
   {
    \l_macmad_argument_i_tl
    \tl_if_blank:VF \l_macmad_argument_ii_tl
     { \{ \l_macmad_argument_ii_tl \} }
   }
  \\*
  \tl_set_rescan:NnV \l_macmad_argument_tl {} \l_macmad_argument_i_tl
  \tl_if_blank:VF \l_macmad_argument_ii_tl
   {
    \tl_set_rescan:NnV \l_macmad_temp_tl {} \l_macmad_argument_ii_tl
    \tl_put_right:Nx \l_macmad_argument_tl { { \exp_not:V \l_macmad_temp_tl } }
   }
  \l_macmad_argument_tl
  \par
 }
\tl_new:N \l_macmad_argument_tl
\tl_new:N \l_macmad_argument_i_tl
\tl_new:N \l_macmad_argument_ii_tl
\tl_new:N \l_macmad_temp_tl
\cs_generate_variant:Nn \tl_set_rescan:Nnn { NnV }
\ExplSyntaxOff

\begin{document}

\begin{command}{\keys}{Shift + F5}
This command allows you to add keyboard strokes.
\end{command}

\begin{command}{\TeX}{}
This command prints the \TeX\ logo.
\end{command}

\begin{command}{\textbf}{\TeX}
This command prints its argument in bold.
\end{command}

\end{document}

在此处输入图片描述

您可以容纳多个参数,但在这种情况下,您需要使用括号括住必填参数,即使只有一个。

\documentclass{article}
\usepackage{fontspec}
\usepackage{menukeys}
\usepackage{xparse} % but it's already loaded by fontspec

\ExplSyntaxOn
% "v" means "verbatim argument"
\NewDocumentEnvironment{command}{vv}
 {
  % store the two arguments in variables
  % xparse has absorbed them "verbatim"
  \tl_set:Nn \l_macmad_argument_i_tl { #1 }
  \tl_set:Nn \l_macmad_argument_ii_tl { #2 }
  \par
  \noindent
  % print the first argument in the margin
  \makebox[0pt][r]{\ttfamily\l_macmad_argument_i_tl\hspace{2em}}
  \ignorespaces
 }
 {
  \par\nopagebreak
  \noindent
  \textbf{Example:~}
  \texttt
   {
    % print the first argument
    \l_macmad_argument_i_tl
    % print the second argument (but only if non empty)
    \tl_if_blank:VF \l_macmad_argument_ii_tl
     { \l_macmad_argument_ii_tl }
   }
  \\*
  % transform back the first argument from verbatim into "standard tokens"
  \tl_set_rescan:NnV \l_macmad_argument_tl {} \l_macmad_argument_i_tl
  \tl_if_blank:VF \l_macmad_argument_ii_tl
   {
    % do the same for the second argument (if non empty)
    \tl_set_rescan:NnV \l_macmad_temp_tl {} \l_macmad_argument_ii_tl
    % append the rescanned text to the previous
    \tl_put_right:Nx \l_macmad_argument_tl { \exp_not:V \l_macmad_temp_tl }
   }
  % process the contents of the rescanned arguments
  \l_macmad_argument_tl
  \par
 }

% allocate the variables
\tl_new:N \l_macmad_argument_tl
\tl_new:N \l_macmad_argument_i_tl
\tl_new:N \l_macmad_argument_ii_tl
\tl_new:N \l_macmad_temp_tl
% generate a variant command
\cs_generate_variant:Nn \tl_set_rescan:Nnn { NnV }
\ExplSyntaxOff

\begin{document}

\begin{command}{\keys}{{Shift + F5}}
This command allows you to add keyboard strokes.
\end{command}

\begin{command}{\TeX}{}
This command prints the \TeX\ logo.
\end{command}

\begin{command}{\textbf}{{\TeX}}
This command prints its argument in bold.
\end{command}

\begin{command}{\framebox}{[3cm][l]{\TeX}}
This command frames text in a specified area.
\end{command}

\end{document}

在此处输入图片描述

答案2

不完全是你问的,但可能对某些人有用。优点是彩色盒子该包可以在具有大量选项的环境中打印 LaTeX 代码及其代码的结果。

平均能量损失

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{menukeys}
\newtcblisting{command}{sidebyside,width=.55\linewidth,nobeforeafter,baseline=5mm,lefthand ratio=0.65}
\parskip1em
\begin{document}

Print the \TeX\ logo \dotfill \begin{command}\TeX\end{command} 

Add keyboard strokes \dotfill \begin{command}\keys{Shift + F5}\end{command}

Print argument in bold \dotfill \begin{command}\textbf{\TeX}\end{command}

\end{document}

编辑

另一个严格但简单的解决方案是包装例子

平均能量损失

\documentclass[a5paper]{article}
\usepackage{menukeys,example,lipsum}
\parindent0in\parskip1em
\begin{document}

This command allows you to add keyboard strokes.%
\begin{example}
\keys{Shift + F5}
\end{example}

This command prints the \TeX\ logo.
\begin{example}
\TeX
\end{example}

This command prints its argument in bold.
\begin{example}
\textbf{\TeX}
\end{example}

This print a boxed dummy text.  

\begin{example}
\fbox{\begin{minipage}{4cm}
\raggedright
\tiny\lipsum[2]
\end{minipage}}
\end{example}

\end{document}

答案3

这应该能满足需要,并且还允许多个参数。唯一的问题是,如果第二个参数中有控制序列,则它们会附加一个空格。

\documentclass{article}
\usepackage{fontspec}
\usepackage{environ}
\usepackage{marginnote}
\usepackage{menukeys}
\NewEnviron{command}[2]{% 
\par
\reversemarginpar\marginnote{\texttt{\string#1}}
\BODY
\par
\textbf{Example:}
\texttt{\string#1\detokenize{#2}}
\par
#1#2
\par
}%


\begin{document}

\begin{command}{\keys}{{Shift + F5}}
This command allows you to add keyboard strokes.
\end{command}

\begin{command}{\TeX}{}
This command prints the \TeX\ logo.
\end{command}

\begin{command}{\textbf}{{\TeX}}
This command prints its argument in bold.
\end{command}

\begin{command}{\rule}{[-2pt]{1em}{1em}}
This command print a black box.
\end{command}

\end{document}

代码产生的输出

相关内容