No se puede utilizar un corchete interior macro o para recortar

Aug 17 2020

Estoy tratando de definir los parámetros opcionales de la figura como predeterminados con un comando de usuario, pero no puedo usarlo dentro de [ ]. El siguiente ejemplo arroja una excepción cuando reemplazo trim=0 0 0 0con trim=\trim.

\usepackage[demo]{graphicx}

\newcommand{\trim}{0 0 0 0}

\begin{document}
    
    \begin{figure}[htbp]
        \centering
        \includegraphics[width=\linewidth,trim=0 0 0 0,clip]{Figure}
        \caption{blablabla.}
    \end{figure}
        
\end{document}

Además, mi objetivo es definir un comando como que pueda reemplazar toda la oración que width=\linewidth,trim=0 0 0 0,clipcontiene [ ].

Respuestas

6 UlrichDiez Aug 17 2020 at 17:02

EDITAR EL 20 DE AGOSTO DE 2020

Una vez más yo (Ulrich Diez) me metí en una situación embarazosa:

Mientras escribía lo siguiente, yo (Ulrich Diez) no pensé en el hecho de que los corchetes [y, a ]diferencia de las llaves, no pertenecen al código de categoría 1 y 2, sino que son caracteres ordinarios del código de categoría 12 y, por lo tanto, los corchetes puede ocurrir dentro de macroargumentos. Menos complicado y mucho más corto que el siguiente código es:

\documentclass{article}

\usepackage[demo]{graphicx}

\newcommand\exchange[2]{#2#1}
\newcommand{\trim}{0 0 0 0}

\begin{document}
    
    \begin{figure}[htbp]
        \centering
        \expandafter\exchange\expandafter{\trim}{%
           \includegraphics[width=\linewidth,trim=%
        },clip]{/path/to/graphics/file.jpg}%%%%%
        % Why two captions?
        \caption{Testing of the Y-axis.}%%%%%
        \caption{blablabla.}%%%%%
    \end{figure}
        
\end{document}

La secuencia

\expandafter\exchange\expandafter{\trim}{%
   \includegraphics[width=\linewidth,trim=%
},clip]{/path/to/graphics/file.jpg}%%%%%

desencadena lo siguiente:

La \expandaftercadena conduce a la expansión \trim:

\exchange{0 0 0 0}{%
   \includegraphics[width=\linewidth,trim=%
},clip]{/path/to/graphics/file.jpg}%%%%%

Expansión de \exchangerendimientos:

\includegraphics[width=\linewidth,trim=%
0 0 0 0,clip]{/path/to/graphics/file.jpg}%%%%%

FIN DE EDICIÓN EL 20 DE AGOSTO DE 2020



Si la definición del \trim-comando puede cambiar de una figura a otra, entonces puede, por ejemplo, usar la \romannumeral0-expansión- y la técnica-de-intercambio-de-argumentos; la esencia de \romannumeral0-expansión es:

  • TeX expande tokens expandibles mientras recolecta tokens que pertenecen a la cantidad de ⟨number⟩ que se va a representar en números romanos.
  • Si el primer token que encuentra TeX mientras reúne la cantidad de ⟨número⟩ es un dígito, por ejemplo, 0entonces el proceso de recopilación de los tokens que pertenecen a la cantidad de ⟨número se convierte en un proceso de recopilación de más dígitos o algo que no es un dígito y, por lo tanto, finaliza el proceso de recopilación. Los tokens expandibles se expanden mientras se acumulan dígitos. Una ficha de espacio que termina una secuencia de dígitos finaliza el proceso de recopilación de más dígitos y se descarta silenciosamente.
  • Si el número recopilado no es positivo, TeX se tragará silenciosamente los tokens que forman la cantidad de ⟨número⟩ sin entregar ningún token a cambio.

Esto implica que \romannumeralse puede usar para engañar a TeX para que haga mucho trabajo de expansión e intercambio de argumentos siempre que se asegure que al final se encuentre un número no positivo.

\documentclass{article}

\usepackage[demo]{graphicx}

\newcommand\exchange[2]{#2#1}
\newcommand{\trim}{0 0 0 0}

\begin{document}
    
    \begin{figure}[htbp]
        \centering
        \expandafter\includegraphics\expandafter[%
          \romannumeral0%
          \expandafter\exchange
          \expandafter{\trim}{ width=\linewidth,trim=},clip%
        ]{/path/to/graphics/file.jpg}%%%%%
        % Why two captions?
        \caption{Testing of the Y-axis.}%%%%%
        \caption{blablabla.}%%%%%
    \end{figure}
        
\end{document}

La secuencia

\expandafter\includegraphics\expandafter[%
  \romannumeral0%
  \expandafter\exchange
  \expandafter{\trim}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

desencadena lo siguiente:

El proceso de obtención de la expansión (de nivel superior) de \expandafterdesencadena el proceso de entrega de la expansión de nivel superior del siguiente token menos uno y termina cuando finaliza el proceso de obtención de la expansión de nivel superior del siguiente token menos uno. . Así:

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggers the process of obtaining the top-level-
% expansion of the next but one token:
\includegraphics\expandafter[%
  \romannumeral0%
  \expandafter\exchange
  \expandafter{\trim}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

El siguiente token que sale del primero \expandaftertambién es un \expandafter, así:

% Process of obtaining the top-level-expansion of the first \expandafter in 
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggers the process of obtaining the top-level-
% expansion of the next but one token:
[%
  \romannumeral0%
  \expandafter\exchange
  \expandafter{\trim}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

El siguiente token que sale del segundo , excepto uno , \expandafteres \romannumeralasí:

% Process of obtaining the top-level-expansion of the first \expandafter in 
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in 
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral in progress;
  %   Sub-process of \romannumeral-expansion for gathering tokens of a <number>-
  %   quantity in progress:
  0%
  \expandafter\exchange
  \expandafter{\trim}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

TeX encuentra el dígito 0, por lo que \romannumeralel subproceso de recopilación de tokens de una cantidad de ⟨número turns se convierte en el proceso de recopilar más dígitos o algo que termina la secuencia de dígitos:

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral in progress;
  %   Sub-process of \romannumeral-expansion for gathering more digits of a
  %   <number>-quantity in progress; digit "0" found so far:
  \expandafter\exchange
  \expandafter{\trim}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

Mientras busca más tokens / dígitos que pertenecen a la cantidad ⟨number⟩ , TeX encuentra el tercero \expandafter:

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral in progress;
  %   Sub-process of \romannumeral-expansion for gathering more digits of a
  %   <number>-quantity in progress; digit "0" found so far:
  %     Process of obtaining the top-level-expansion of the third \expandafter
  %     in progress, this process triggers the process of obtaining the top-
  %     level-expansion of the next but one token:
  \exchange
  \expandafter{\trim}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

El siguiente token que sale del tercero \expandafteres \expandafter, por tanto:

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral in progress;
  %   Sub-process of \romannumeral-expansion for gathering more digits of a
  %   <number>-quantity in progress; digit "0" found so far:
  %     Process of obtaining the top-level-expansion of the third \expandafter
  %     in progress, this process triggerd the process of obtaining the top-
  %     level-expansion of the fourth \expandafter:
  \exchange
  %   Process of obtaining the top-level-expansion of the fourth \expandafter
  %   in progress, this process triggers the process of obtaining the top-
  %   level-expansion of the next but one token:
  {\trim}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

La siguiente ficha que sale del cuarto \expandafteres \trim, por lo tanto, el proceso de obtención de la expansión de nivel superior del cuarto \expandafterfinaliza cuando finaliza el proceso de obtención de la expansión de nivel superior de \trimes:

% Process of obtaining the top-level-expansion of the first \expandafter in 
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral in progress;
  %   Sub-process of \romannumeral-expansion for gathering more digits of a 
  %   <number>-quantity in progress, digit "0" found so far:
  %     Process of obtaining the top-level-expansion of the third \expandafter
  %     in progress, this process triggerd the process of obtaining the top-
  %     level-expansion of the fourth \expandafter:
  \exchange
  %  Process of obtaining the top-level-expansion of the fourth \expandafter
  %  terminated.
  {0 0 0 0}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

A medida que \expandafterfinaliza el proceso de obtención de la expansión de nivel superior del cuarto , también termina el proceso de obtención de la expansión de nivel superior del tercero \expandafter:

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral in progress;
  %   Sub-process of \romannumeral-expansion for gathering more digits of a
   %  <number>-quantity in progress, digit "0" found so far:
  %     Process of obtaining the top-level-expansion of the third \expandafter
  %     terminated.
  \exchange
  {0 0 0 0}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

A medida que \expandafterfinaliza el proceso de obtención de la expansión de nivel superior del tercero , la \romannumeralexpansión continúa, lo que produce la expansión \exchange:

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral in progress;
  %   Sub-process of \romannumeral-expansion for gathering more digits of a
  %   <number>-quantity in progress; digit "0" found so far:
  <space-token>width=\linewidth,trim=0 0 0 0,clip%
]{/path/to/graphics/file.jpg}%%%%%

TeX encuentra una ficha de espacio. Esa ficha de espacio termina el subproceso de recopilar más dígitos de una cantidad de " números" y se descarta en silencio.

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral in progress;
  %   Sub-process of \romannumeral-expansion for gathering more digits of a
  %   <number>-quantity terminated; number "0" found.
  width=\linewidth,trim=0 0 0 0,clip%
]{/path/to/graphics/file.jpg}%%%%%

Como TeX encontró una cantidad de ⟨número⟩ cuyo valor es 0, mientras 0que no es un valor positivo, el proceso de obtener la expansión de nivel superior de \romannumeraltermina al tragar silenciosamente los tokens que forman esa cantidad de ⟨número⟩ sin que TeX entregue ningún token en regreso.

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of \romannumeral:
[%
  % Process of obtaining the top-level-expansion of \romannumeral terminated.
  width=\linewidth,trim=0 0 0 0,clip%
]{/path/to/graphics/file.jpg}%%%%%

Cuando \romannumeralfinaliza el proceso de obtención de la expansión de nivel superior de \expandafter, también termina el proceso de obtención de la expansión de nivel superior del segundo :

% Process of obtaining the top-level-expansion of the first \expandafter in
% progress, this process triggerd the process of obtaining the top-level-
% expansion of the second \expandafter:
\includegraphics
% Process of obtaining the top-level-expansion of the second \expandafter
% terminated.
[%
  width=\linewidth,trim=0 0 0 0,clip%
]{/path/to/graphics/file.jpg}%%%%%

Cuando \expandafterfinaliza el proceso de obtención de la expansión de nivel superior del segundo , también termina el proceso de obtención de la expansión de nivel superior del primero \expandafter:

% Process of obtaining the top-level-expansion of the first \expandafter
% terminated.
\includegraphics
[%
  width=\linewidth,trim=0 0 0 0,clip%
]{/path/to/graphics/file.jpg}%%%%%