Mi intento

Mi intento

ACTUALIZACIÓN: Abrí una pregunta de seguimiento:¿Cómo puedo componer un entorno y su equivalente literal en un entorno?


Me gustaría pasar un comando y argumentos a un entorno. El resultado debe ser triple:

  1. El comando literal
  2. La versión tipográfica (cómo debería aparecer cuando se usa)
  3. La definición del comando. (Esta es sólo una descripción textual de para qué se utiliza el comando).

Mi intento

Para este ejemplo, intento utilizar el \keycomando proporcionado por elmenukeyspaquete.

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

Salida deseada

ingrese la descripción de la imagen aquí

Mis pensamientos

Quizás este ni siquiera sea el enfoque correcto. No sé cómo manejar los comandos con múltiples argumentos de manera eficiente (por ejemplo, creados con el xparsepaquete).

Respuesta1

Debes absorber los dos argumentos palabra por palabra, lo que xparsepermite hacerlo; luego puede “volver a escanear” los dos argumentos cuando desee mostrar el efecto.

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

ingrese la descripción de la imagen aquí

Puede acomodar más de un argumento, pero en este caso necesita usar llaves para los obligatorios, incluso si solo hay uno de ellos.

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

ingrese la descripción de la imagen aquí

Respuesta2

No es exactamente lo que preguntaste, pero puede ser útil para alguien. La ventaja es que elcaja de coloresEl paquete puede imprimir el código LaTeX y el resultado de este código en un entorno con muchas opciones.

MWE

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

Editar:

Otra solución rígida pero sencilla es el paquete.ejemplo:

MWE

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

Respuesta3

Esto debería funcionar como se desea y también permite múltiples argumentos. El único problema es que si hay secuencias de control en el segundo argumento, se añaden un espacio.

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

La salida producida por el código.

información relacionada