ブラケット内またはトリミングにマクロを使用することはできません

Aug 17 2020

ユーザーコマンドを使用して、Figureのオプションのパラメーターをデフォルトとして定義しようとしていますが、内で使用できません[ ]。次の例では、をに置き換えると例外がスローさtrim=0 0 0 0trim=\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}

その上、私の目的は、width=\linewidth,trim=0 0 0 0,clip中の文全体[ ]をそれに置き換えることができるようなコマンドを定義することです。

回答

6 UlrichDiez Aug 17 2020 at 17:02

2020年8月20日に編集

もう一度私(Ulrich Diez)は恥ずかしい状況に身を置きました:

以下のことを書いている間、I(Ulrich Diez)は、角括弧[](中括弧とは異なり)がカテゴリコード1と2ではなく、カテゴリコード12の通常の文字であり、したがって角括弧であるという事実については考えませんでした。マクロ引数内で発生する可能性があります。以下のコードよりも複雑でなく、はるかに短いです。

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

シーケンス

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

以下をトリガーします。

-\expandafterチェーンは拡大につながります\trim

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

\exchange歩留まりの拡大:

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

2020年8月20日の編集終了



\trim-コマンドの定義が図\romannumeral0ごとに変わる可能性がある場合は、たとえば、-expansion-およびargument-exchanging-techniqueを使用できます。- \romannumeral0expansionの要点は次のとおりです。

  • TeXは、ローマ数字で表される⟨number⟩ -quantityに属するトークンを収集しながら、拡張可能なトークンを拡張します。
  • TeXが⟨number⟩- quantityの収集中に最初に見つけたトークンが数字である場合、たとえば、⟨number⟩ -quantityに0属するトークンを収集するプロセスは、より多くの数字またはそうでないものを収集するプロセスに変わります。数字であるため、収集プロセスを終了します。拡張可能なトークンは、数字を収集するときに拡張されます。ディジットシーケンスを終了するスペーストークンは、より多くのディジットを収集するプロセスを終了し、サイレントに破棄されます。
  • 収集された数が正でない場合、TeXは、トークンを提供せずに、⟨number⟩ -quantityを形成するトークンをサイレントに飲み込みます。

これ\romannumeralは、最終的に正でない数が見つかることが保証されている限り、TeXをだまして多くの拡張および引数交換作業を実行させるために使用できることを意味します。

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

シーケンス

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

以下をトリガーします。

の(トップレベル)拡張を取得するプロセスは\expandafter、次の1つのトークンのトップレベル拡張を配信するプロセスをトリガーし、次の1つのトークンのトップレベル拡張を取得するプロセスが終了すると終了します。 。したがって:

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

最初から発信される次の1つのトークン\expandafter\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 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}%%%%%

第二の次が、1つのトークンの発信が\expandafterあり\romannumeral、したがって、:

% 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は数字を見つける0ので\romannumeral⟨number⟩ -quantityのトークンを収集するサブプロセスは、より多くの数字を収集するプロセス、または数字シーケンスを終了する何かに変わります。

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

⟨number⟩ -quantityに属するトークン/数字をさらに検索しているときに、TeXは3番目に遭遇し\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}%%%%%

第三の次が、1つのトークンの発信が\expandafterあり\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 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}%%%%%

4番目から送信される次の1つのトークン\expandafterはです\trim。したがって、4番目のトップレベル拡張\expandafterを取得するプロセスは、のトップレベル拡張を取得するプロセス\trimが終了すると終了します。

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

4番目\expandafterのトップレベル拡張を取得するプロセスが終了すると、3番目のトップレベル拡張を取得するプロセス\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}%%%%%

3番目のトップレベル拡張を取得するプロセス\expandafterが終了すると、\romannumeral-expansionが続行され、拡張が発生し\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はスペーストークンを見つけます。そのスペーストークンは、⟨number⟩量のより多くの桁を収集するサブプロセスを終了し、サイレントに破棄されます。

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

TeXが正の値ではないが値がである⟨number⟩- quantityを見つけたため、TeXがトークンを配信せずに、その⟨number⟩ -quantityを形成するトークンをサイレントに飲み込むことにより、のトップレベルの拡張を取得するプロセスが終了します。戻る。00\romannumeral

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

のトップレベル拡張\romannumeralを取得するプロセスが終了すると、2番目のトップレベル拡張を取得するプロセス\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}%%%%%

2番目\expandafterのトップレベルの拡張を取得するプロセスが終了すると、最初のトップレベルの拡張を取得するプロセス\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}%%%%%