我的嘗試

我的嘗試

更新:我提出了一個後續問題:如何排版環境及其在環境中的等效文字?


我想將命令和參數傳遞給環境。輸出應該是三重:

  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}

程式碼產生的輸出

相關內容