Tidak dapat menggunakan makro di dalam kurung atau untuk pemangkasan
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 0
dengan 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,clip
di dalamnya [ ]
dengannya.
Jawaban
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 \exchange
hasil:
\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 \romannumeral0
teknik -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,
0
maka 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 \romannumeral
dapat 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-) \expandafter
memicu 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 \expandafter
juga 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 \expandafter
adalah \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 \romannumeral
sub-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 \expandafter
adalah \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 \expandafter
adalah \trim
, dengan demikian proses mendapatkan ekspansi tingkat atas dari yang keempat \expandafter
berakhir ketika proses mendapatkan perluasan tingkat atas \trim
berakhir:
% 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 \expandafter
dihentikan, proses mendapatkan perluasan tingkat atas dari yang ketiga juga \expandafter
berakhir:
% 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 \expandafter
dihentikan, \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 0
bukan nilai positif, proses untuk mendapatkan perluasan tingkat atas dari \romannumeral
penghentian 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 \romannumeral
dihentikan, proses mendapatkan perluasan tingkat atas dari yang kedua juga \expandafter
berakhir:
% 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 \expandafter
dihentikan, proses mendapatkan perluasan tingkat atas yang pertama juga \expandafter
berakhir:
% 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}%%%%%