Non è possibile utilizzare una macro all'interno della parentesi o per il taglio

Aug 17 2020

Sto cercando di definire i parametri opzionali della figura come predefiniti con un comando utente ma non posso usarlo all'interno del file [ ]. Il prossimo esempio genera un'eccezione quando sostituisco 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}

Inoltre, il mio obiettivo è definire un comando in modo da poter sostituire l'intera frase width=\linewidth,trim=0 0 0 0,clipal suo interno [ ].

Risposte

6 UlrichDiez Aug 17 2020 at 17:02

MODIFICA IL 20 AGOSTO 2020

Ancora una volta io (Ulrich Diez) mi sono messo in una situazione imbarazzante:

Mentre scrivevo le cose di seguito, io (Ulrich Diez) non ho pensato al fatto che le parentesi quadre [e - a ]differenza delle parentesi graffe - non sono del codice di categoria 1 e 2 ma sono caratteri ordinari del codice di categoria 12 e che quindi le parentesi quadre può verificarsi all'interno di macro-argomenti. Meno complicato e molto più breve del codice seguente è:

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

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

attiva quanto segue:

La \expandafter-chain porta all'espansione \trim:

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

\exchangeRendimenti in espansione :

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

FINE MODIFICA IL 20 AGOSTO 2020



Se la definizione del \trim-command può cambiare da figura a figura, allora puoi, ad esempio, usare la \romannumeral0-expansion- e la tecnica dello scambio di argomenti - il succo di \romannumeral0-expansion è:

  • TeX espande gettoni espandibili mentre raccogliendo gettoni che appartengono al ⟨number⟩ -Quantità che deve essere rappresentato in numeri romani.
  • Se il primo token che TeX trova mentre raccoglie la quantità del ⟨numero⟩ è una cifra, ad esempio, 0allora il processo di raccolta dei token che appartengono alla quantità del ⟨numero⟩ si trasforma in un processo di raccolta di più cifre o qualcosa che non lo è una cifra e quindi termina il processo di raccolta. I token espandibili vengono espansi durante la raccolta delle cifre. Un segno di spazio che termina una sequenza di cifre termina il processo di raccolta di più cifre e viene silenziosamente scartato.
  • Se il numero raccolti non è positivo, TeX silenziosamente ingoiare i token che formano la ⟨number⟩ -Quantità senza consegnare alcun segno in cambio.

Ciò implica che \romannumeralpuò essere usato per indurre TeX a fare un sacco di lavoro di espansione e scambio di argomenti purché sia ​​assicurato che alla fine venga trovato un numero non 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 sequenza

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

attiva quanto segue:

Il processo per ottenere l'espansione (di primo livello) di \expandafterinnesca il processo di consegna dell'espansione di livello superiore del token successivo tranne uno e termina quando termina il processo di ottenimento dell'espansione di livello superiore del token successivo ma uno . Quindi:

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

Anche il successivo token in uscita dal primo \expandafterè un \expandafter, quindi:

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

Il prossimo ma un token in uscita dal secondo \expandafterè \romannumeral, quindi:

% 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 trova la cifra 0, così \romannumeral's sub-processo di raccolta di gettoni di un ⟨number⟩ -Quantità giri nel processo di raccolta più cifre o qualcosa che terminano la cifra sequenza:

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

Durante la ricerca di più token / cifre appartenenti alla quantità ⟨numero⟩ , TeX incontra il terzo \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}%%%%%

Il prossimo ma un token in uscita dal terzo \expandafterè \expandafter, quindi:

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

Il prossimo ma un token in uscita dal quarto \expandafterè \trim, quindi il processo per ottenere l'espansione di livello superiore del quarto \expandaftertermina quando termina il processo per ottenere l'espansione di livello superiore di \trimis:

% 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 il processo per ottenere l'espansione di livello superiore della quarta \expandafterè terminato, termina anche il processo per ottenere l'espansione di livello superiore della terza \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}%%%%%

Quando il processo per ottenere l'espansione di livello superiore del terzo \expandafterè terminato, l' \romannumeralespansione continua, il che si traduce in espansione \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 trova un gettone spaziale. Che termina spazio-token il sub-processo di raccolta più cifre di un ⟨number⟩ -Quantità e viene silenziosamente scartato.

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

Poiché TeX ha trovato una quantità ⟨numero⟩ il cui valore è 0, mentre 0non è un valore positivo, il processo per ottenere l'espansione di livello superiore di \romannumeraltermina inghiottendo silenziosamente i token che formano quella quantità ⟨numero⟩ senza che TeX fornisca alcun token in ritorno.

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

Quando il processo per ottenere l'espansione di livello superiore di \romannumeralè terminato, termina anche il processo per ottenere l'espansione di livello superiore del secondo \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
% terminated.
[%
  width=\linewidth,trim=0 0 0 0,clip%
]{/path/to/graphics/file.jpg}%%%%%

Quando il processo per ottenere l'espansione di livello superiore del secondo \expandafterè terminato, anche il processo per ottenere l'espansione di livello superiore del primo \expandaftertermina:

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