Não é possível usar uma macro entre colchetes ou para aparar

Aug 17 2020

Estou tentando definir os parâmetros opcionais da figura como padrão com um comando do usuário, mas não consigo usá-lo dentro do [ ]. O próximo exemplo lança uma exceção quando substituo o trim=0 0 0 0por 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}

Além disso, meu objetivo é definir um comando como eu posso substituir toda a frase width=\linewidth,trim=0 0 0 0,clipdentro [ ]dele.

Respostas

6 UlrichDiez Aug 17 2020 at 17:02

EDITAR EM 20 DE AGOSTO DE 2020

Mais uma vez eu (Ulrich Diez) me coloco em uma situação embaraçosa:

Enquanto escrevia as coisas abaixo, eu (Ulrich Diez) não pensei sobre o fato de que colchetes [e - ao ]contrário de colchetes - não são do código de categoria 1 e 2, mas são caracteres comuns do código de categoria 12 e, portanto, colchetes pode ocorrer dentro de macro-argumentos. Menos complicado e muito mais curto do que o código abaixo é:

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

A sequência

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

aciona o seguinte:

A \expandaftercadeia leva à expansão \trim:

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

Expansão dos \exchangerendimentos:

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

FIM DA EDIÇÃO EM 20 DE AGOSTO DE 2020



Se a definição do \trim-comando pode mudar de figura para figura, então você pode, por exemplo, usar a \romannumeral0-expansão- e técnica-de-troca de argumentos - a essência da \romannumeral0-expansão é:

  • O TeX expande tokens expansíveis enquanto reúne tokens que pertencem à quantidade ⟨number⟩ que deve ser representada em algarismos romanos.
  • Se o primeiro token que o TeX encontra ao reunir a quantidade ⟨número⟩ for um dígito, por exemplo, 0então o processo de coleta de tokens que pertencem à quantidade tonúmero se transforma em um processo de coleta de mais dígitos ou algo que não é um dígito e, portanto, encerra o processo de coleta. Os tokens expansíveis são expandidos durante a coleta de dígitos. Um token de espaço encerrando uma sequência de dígitos encerra o processo de coleta de mais dígitos e é descartado silenciosamente.
  • Se o número coletado não for positivo, o TeX engolirá silenciosamente os tokens que formam a quantidade ⟨number⟩ sem entregar nenhum token em troca.

Isso implica que \romannumeralpode ser usado para enganar o TeX para fazer um grande trabalho de expansão e troca de argumentos, desde que seja garantido que no final um número não positivo seja encontrado.

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

A sequência

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

aciona o seguinte:

O processo de obtenção da expansão de nível superior de \expandafteraciona o processo de entrega da expansão de nível superior do próximo, exceto um token e termina quando o processo de obtenção da expansão de nível superior do próximo, exceto um token termina . Portanto:

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

O próximo, mas um token saindo do primeiro \expandaftertambém é um \expandafter, assim:

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

O próximo token que sai do segundo \expandafteré \romannumeral, assim:

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

O TeX encontra o dígito 0, portanto \romannumeral, o subprocesso de coleta de tokens de uma quantidade de ⟨número⟩ se transforma no processo de coleta de mais dígitos ou algo que termina a sequência 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}%%%%%

Ao pesquisar mais tokens / dígitos pertencentes à quantidade ⟨number⟩ , o TeX encontra o terceiro \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}%%%%%

O próximo token que sai do terceiro \expandafteré \expandafter, assim:

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

O próximo, mas um token que sai do quarto \expandafteré \trim, portanto, o processo de obtenção da expansão de nível superior do quarto \expandaftertermina quando o processo de obtenção da expansão de nível superior de \trimé termina:

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

Quando o processo de obtenção da expansão de nível superior do quarto \expandafteré encerrado, o processo de obtenção da expansão de nível superior do terceiro também \expandaftertermina:

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

À medida que o processo de obtenção da expansão de nível superior do terceiro \expandafteré encerrado, a \romannumeralexpansão continua, o que resulta em expansão \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 encontra um token de espaço. Esse token de espaço termina o subprocesso de coleta de mais dígitos de uma quantidade ⟨number e é descartado silenciosamente.

% 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 o TeX encontrou uma quantidade ⟨número⟩ cujo valor é 0, embora 0não seja um valor positivo, o processo de obtenção da expansão de nível superior de \romannumeraltermina engolindo silenciosamente os tokens formando essa quantidade ⟨número⟩ sem que o TeX entregue qualquer token em Retorna.

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

À medida que o processo de obtenção da expansão de nível superior de \romannumeralé encerrado, o processo de obtenção da expansão de nível superior do segundo \expandaftertermina também:

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

À medida que o processo de obtenção da expansão de nível superior do segundo \expandafteré encerrado, o processo de obtenção da expansão de nível superior do primeiro \expandaftertermina também:

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