Tidak dapat menggunakan makro di dalam kurung atau untuk pemangkasan

Aug 17 2020

Saya mencoba untuk mendefinisikan parameter opsional gambar sebagai default dengan perintah pengguna tetapi saya tidak dapat menggunakannya di dalam [ ]. Contoh berikutnya menampilkan pengecualian saat saya mengganti trim=0 0 0 0dengan 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}

Selain itu, tujuan saya adalah untuk mendefinisikan sebuah perintah seperti saya dapat mengganti seluruh kalimat width=\linewidth,trim=0 0 0 0,clipdi dalamnya [ ]dengannya.

Jawaban

6 UlrichDiez Aug 17 2020 at 17:02

EDIT TANGGAL 20 AGUSTUS 2020

Sekali lagi saya (Ulrich Diez) menempatkan diri saya dalam situasi yang memalukan:

Saat menulis hal-hal di bawah ini, saya (Ulrich Diez) tidak memikirkan fakta bahwa tanda kurung siku [dan ]—tidak seperti tanda kurung kurawal — bukan dari kode kategori 1 dan 2 tetapi merupakan karakter biasa dari kode kategori 12 dan oleh karena itu tanda kurung siku dapat terjadi dalam argumen makro. Lebih mudah dan lebih pendek dari kode di bawah ini:

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

Urutannya

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

memicu hal berikut:

The \expandafter-rantai mengarah ke perluasan \trim:

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

Memperluas \exchangehasil:

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

AKHIR EDIT PADA 20 AGUSTUS 2020



Jika definisi \trim-command dapat berubah dari gambar ke gambar, maka Anda dapat, misalnya, menggunakan \romannumeral0teknik -expansion- dan argumen-pertukaran-inti dari \romannumeral0-expansion adalah:

  • TeX memperluas token yang dapat diperluas sambil mengumpulkan token yang termasuk dalam kuantitas ⟨number⟩ yang akan direpresentasikan dalam angka romawi.
  • Jika token pertama yang TeX temukan saat mengumpulkan kuantitas ⟨nomor⟩ adalah digit, misalnya, 0maka proses pengumpulan token yang termasuk dalam kuantitas ⟨bilangan⟩ berubah menjadi proses mengumpulkan lebih banyak digit atau sesuatu yang bukan satu digit dan karenanya menghentikan proses pengumpulan. Token yang dapat diperluas diperluas sambil mengumpulkan angka. Token spasial yang menghentikan urutan digit menghentikan proses pengumpulan lebih banyak digit dan dibuang secara diam-diam.
  • Jika nomor yang dikumpulkan tidak positif, TeX akan diam-diam menelan token yang membentuk kuantitas ⟨nomor⟩ tanpa memberikan token apa pun sebagai imbalan.

Ini menyiratkan bahwa \romannumeraldapat digunakan untuk mengelabui TeX agar melakukan banyak pekerjaan ekspansi- dan pertukaran argumen selama dipastikan bahwa pada akhirnya ditemukan bilangan non-positif.

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

Urutannya

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

memicu hal berikut:

Proses mendapatkan perluasan (tingkat atas-) \expandaftermemicu proses pengiriman perluasan tingkat atas dari token berikutnya tetapi satu dan berakhir ketika proses mendapatkan perluasan tingkat atas dari yang berikutnya tetapi satu token berakhir . Jadi:

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

Token berikutnya tetapi satu yang keluar dari yang pertama \expandafterjuga merupakan \expandafter, jadi:

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

Token berikutnya tetapi keluar dari yang kedua \expandafteradalah \romannumeral, dengan demikian:

% 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 menemukan digit tersebut 0, sehingga \romannumeralsub-proses pengumpulan token dari kuantitas ⟨nomor⟩ berubah menjadi proses mengumpulkan lebih banyak digit atau sesuatu yang menghentikan urutan digit:

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

Saat mencari lebih banyak token / digit yang termasuk dalam kuantitas ⟨number⟩ , TeX menemukan yang ketiga \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}%%%%%

Token berikutnya kecuali yang keluar dari yang ketiga \expandafteradalah \expandafter, sebagai berikut:

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

Token berikutnya tetapi satu yang keluar dari yang keempat \expandafteradalah \trim, dengan demikian proses mendapatkan ekspansi tingkat atas dari yang keempat \expandafterberakhir ketika proses mendapatkan perluasan tingkat atas \trimberakhir:

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

Karena proses mendapatkan perluasan tingkat atas dari yang keempat \expandafterdihentikan, proses mendapatkan perluasan tingkat atas dari yang ketiga juga \expandafterberakhir:

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

Saat proses mendapatkan ekspansi tingkat atas dari yang ketiga \expandafterdihentikan, \romannumeral-pengembangan berlanjut, yang menghasilkan perluasan \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 menemukan token spasial. Space-token menghentikan sub-proses mengumpulkan lebih banyak digit dari sebuah kuantitas ⟨number⟩ dan akan dibuang secara diam-diam.

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

Karena TeX menemukan kuantitas ⟨nomor⟩ yang nilainya 0, sementara 0bukan nilai positif, proses untuk mendapatkan perluasan tingkat atas dari \romannumeralpenghentian dengan diam-diam menelan token yang membentuk kuantitas ⟨bilangan⟩ tanpa TeX mengirimkan token apa pun di kembali.

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

Saat proses mendapatkan perluasan tingkat atas dari \romannumeraldihentikan, proses mendapatkan perluasan tingkat atas dari yang kedua juga \expandafterberakhir:

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

Karena proses mendapatkan perluasan tingkat atas yang kedua \expandafterdihentikan, proses mendapatkan perluasan tingkat atas yang pertama juga \expandafterberakhir:

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