TransWikia.com

Can't use a macro inside bracket or for trimming

TeX - LaTeX Asked by Ernesto Iglesias on September 9, 2020

I’m trying to define figure optional parameters as a default with a user command but I can’t use it inside the [ ]. The next example throws an exception when I replace the trim=0 0 0 0 with 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{Testing of the Y-axis.}
        caption{blablabla.}
    end{figure}
        
end{document}

Besides, my objective is to define a command such as I can replace the whole sentence width=linewidth,trim=0 0 0 0,clip inside [ ] with it.

One Answer

EDIT ON AUGUST 20, 2020

Once more I(Ulrich Diez) put myself into an embarrassing situation:

While writing the things below, I(Ulrich Diez) didn't think about the fact that square-brackets [ and ]—unlike curly braces—are not of category code 1 and 2 but are ordinary characters of category code 12 and that therefore square-brackets can occur within macro-arguments. Less complicated and much shorter than the code below is:

documentclass{article}

usepackage[demo]{graphicx}

newcommandexchange[2]{#2#1}
newcommand{trim}{0 0 0 0}

begin{document}
    
    begin{figure}[htbp]
        centering
        expandafterexchangeexpandafter{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}

The sequence

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

triggers the following:

The expandafter-chain leads to expanding trim:

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

Expanding exchange yields:

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

END OF EDIT ON AUGUST 20, 2020



If the definition of the trim-command can change from figure to figure, then you can, e.g., use the romannumeral0-expansion- and argument-exchanging-technique—the gist of romannumeral0-expansion is:

  • TeX expands expandable tokens while gathering tokens that belong to the ⟨number⟩-quantity that is to be represented in roman numerals.
  • If the first token which TeX finds while gathering the ⟨number⟩-quantity is a digit, e.g., 0, then the process of gathering tokens that belong to the ⟨number⟩-quantity turns into a process of gathering more digits or something that is not a digit and therefore terminates the process of gathering. Expandable tokens get expanded while gathering digits. A space-token terminating a digit-sequence terminates the process of gathering more digits and gets silently discarded.
  • If the number gathered is not positive, TeX will silently swallow the tokens forming the ⟨number⟩-quantity without delivering any token in return.

This implies that romannumeral can be used for tricking TeX into doing a lot of expansion- and argument-exchanging-work as long as it is ensured that in the end a non-positive number is found.

documentclass{article}

usepackage[demo]{graphicx}

newcommandexchange[2]{#2#1}
newcommand{trim}{0 0 0 0}

begin{document}
    
    begin{figure}[htbp]
        centering
        expandafterincludegraphicsexpandafter[%
          romannumeral0%
          expandafterexchange
          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}

The sequence

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

triggers the following:

The process of obtaining the (top-level-)expansion of expandafter triggers the process of delivering the top-level-expansion of the next but one token and terminates when the process of obtaining the top-level-expansion of the next but one token terminates. Thus:

% 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:
includegraphicsexpandafter[%
  romannumeral0%
  expandafterexchange
  expandafter{trim}{ width=linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

The next but one token outgoing from the first expandafter is also an expandafter, thus:

% 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%
  expandafterexchange
  expandafter{trim}{ width=linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

The next but one token outgoing from the second expandafter is romannumeral, thus:

% 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%
  expandafterexchange
  expandafter{trim}{ width=linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

TeX finds the digit 0, thus romannumeral's sub-process of gathering tokens of a ⟨number⟩-quantity turns into the process of gathering more digits or something that terminates the digit-sequence:

% 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:
  expandafterexchange
  expandafter{trim}{ width=linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

While searching more tokens/digits belonging to the ⟨number⟩-quantity, TeX encounters the third 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}%%%%%

The next but one token outgoing from the third expandafter is expandafter, thus:

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

The next but one token outgoing from the fourth expandafter is trim, thus the process of obtaining the top-level-expansion of the fourth expandafter terminates when the process of obtaining the top-level-expansion of trim is terminates:

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

As the process of obtaining the top-level-expansion of the fourth expandafter is terminated, the process of obtaining the top-level-expansion of the third expandafter terminates, too:

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

As the process of obtaining the top-level-expansion of the third expandafter is terminated, romannumeral-expansion continues, which yields expanding 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 finds a space-token. That space-token terminates the sub-process of gathering more digits of a ⟨number⟩-quantity and gets silently discarded.

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

As TeX found a ⟨number⟩-quantity whose value is 0, while 0 is not a positive value, the process of obtaining the top-level-expansion of romannumeral terminates by silently swallowing the tokens forming that ⟨number⟩-quantity without TeX delivering any token in return.

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

As the process of obtaining the top-level-expansion of romannumeral is terminated, the process of obtaining the top-level-expansion of the second expandafter terminates, too:

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

As the process of obtaining the top-level-expansion of the second expandafter is terminated, the process of obtaining the top-level-expansion of the fiirst expandafter terminates, too:

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

Answered by Ulrich Diez on September 9, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP