Không thể sử dụng macro bên trong dấu ngoặc hoặc để cắt

Aug 17 2020

Tôi đang cố gắng xác định các tham số tùy chọn hình làm mặc định bằng lệnh người dùng nhưng tôi không thể sử dụng nó bên trong [ ]. Ví dụ tiếp theo ném một ngoại lệ khi tôi thay thế trim=0 0 0 0bằng 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}

Bên cạnh đó, mục tiêu của tôi là xác định một lệnh chẳng hạn như tôi có thể thay thế toàn bộ câu width=\linewidth,trim=0 0 0 0,clipbên trong [ ]bằng nó.

Trả lời

6 UlrichDiez Aug 17 2020 at 17:02

CHỈNH SỬA VÀO NGÀY 20 THÁNG 8 NĂM 2020

Một lần nữa tôi (Ulrich Diez) lại tự đặt mình vào tình huống xấu hổ:

Trong khi viết những điều dưới đây, tôi (Ulrich Diez) đã không nghĩ đến thực tế là dấu ngoặc vuông []—không giống như dấu ngoặc nhọn — không thuộc mã loại 1 và 2 mà là các ký tự bình thường của mã loại 12 và do đó dấu ngoặc vuông có thể xảy ra trong các đối số macro. Ít phức tạp hơn và ngắn hơn nhiều so với mã dưới đây là:

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

Trình tự

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

kích hoạt những điều sau:

Các \expandafterdẫn -chain mở rộng \trim:

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

Mở rộng \exchangesản lượng:

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

KẾT THÚC CHỈNH SỬA VÀO NGÀY 20 THÁNG 8 NĂM 2020



Nếu định nghĩa của \trim-command có thể thay đổi từ hình này sang hình khác, thì bạn có thể, ví dụ: sử dụng \romannumeral0kỹ thuật-mở rộng- và đối số-trao đổi - ý chính của- \romannumeral0mở rộng là:

  • TeX mở rộng các mã thông báo có thể mở rộng trong khi thu thập các mã thông báo thuộc về số lượng ⟨number⟩ được biểu thị bằng số la mã.
  • Ví dụ: nếu mã thông báo đầu tiên mà TeX tìm thấy trong khi thu thập số lượng ⟨number⟩ là một chữ số, 0thì quá trình thu thập mã thông báo thuộc về số lượng ⟨number⟩ sẽ chuyển thành quá trình thu thập nhiều chữ số hơn hoặc thứ gì đó không một chữ số và do đó kết thúc quá trình thu thập. Mã thông báo có thể mở rộng được mở rộng trong khi thu thập các chữ số. Dấu cách mã thông báo kết thúc một chuỗi chữ số sẽ chấm dứt quá trình thu thập nhiều chữ số hơn và bị loại bỏ một cách âm thầm.
  • Nếu số lượng thu thập được không phải là số dương, TeX sẽ âm thầm nuốt các mã thông báo tạo thành số lượng ⟨number⟩ mà không chuyển lại bất kỳ mã thông báo nào.

Điều này ngụ ý rằng nó \romannumeralcó thể được sử dụng để lừa TeX thực hiện nhiều công việc mở rộng- và đối số- trao đổi- miễn là nó được đảm bảo rằng cuối cùng tìm thấy một số không dương.

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

Trình tự

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

kích hoạt những điều sau:

Quá trình lấy mở rộng (cấp cao nhất-) của \expandafterkích hoạt quá trình phân phối mở rộng cấp cao nhất của mã thông báo tiếp theo nhưng kết thúc và kết thúc khi quá trình lấy mở rộng cấp cao nhất của mã thông báo tiếp theo nhưng một mã thông báo kết thúc . Như vậy:

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

Mã tiếp theo nhưng một mã đi ra từ mã đầu tiên \expandaftercũng là một \expandafter, do đó:

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

Các đi dấu hiệu tiếp theo nhưng một từ thứ hai \expandafter\romannumeral, như sau:

% 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 tìm thấy chữ số 0, do đó \romannumeralquá trình phụ thu thập các mã thông báo của một số ⟨number⟩ chuyển thành quá trình thu thập nhiều chữ số hơn hoặc một cái gì đó kết thúc dãy chữ số:

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

Trong khi tìm kiếm nhiều mã thông báo / chữ số hơn thuộc về số lượng ⟨number⟩, TeX gặp phải thứ ba \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}%%%%%

Các đi dấu hiệu tiếp theo nhưng một từ thứ ba \expandafter\expandafter, như sau:

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

Tiếp theo, trừ một mã thông báo đi từ mã thứ tư \expandafter\trim, do đó quá trình lấy mở rộng cấp cao nhất của mã thứ tư \expandafterkết thúc khi quá trình lấy mở rộng cấp cao nhất \trimkết thúc:

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

Khi quá trình nhận được mở rộng cấp cao nhất của thứ tư \expandafterkết thúc, quá trình nhận được mở rộng cấp cao nhất của thứ ba cũng \expandafterkết thúc:

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

Khi quá trình đạt được sự mở rộng cấp cao nhất của phần thứ ba \expandafterbị chấm dứt, \romannumeral-expansion tiếp tục, dẫn đến việc mở rộng \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 tìm thấy một mã thông báo không gian. Không gian-token đó chấm dứt quá trình con thu thập thêm các chữ số của một số ⟨number⟩ và bị loại bỏ một cách âm thầm.

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

Như TeX tìm thấy một ⟨number⟩ -Số lượng có giá trị là 0, trong khi 0không phải là một giá trị tích cực, quá trình lấy top-level-mở rộng \romannumeralchấm dứt bởi âm thầm nuốt các thẻ hình thành mà ⟨number⟩ -Số lượng mà không TeX cung cấp bất kỳ trong thẻ trở về.

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

Khi quá trình lấy mở rộng cấp cao nhất của \romannumeralkết thúc, quá trình lấy mở rộng cấp cao nhất của thứ hai cũng \expandafterkết thúc:

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

Khi quá trình lấy bản mở rộng cấp cao nhất của thứ hai \expandafterkết thúc, quá trình lấy bản mở rộng cấp cao nhất của phần đầu tiên cũng \expandafterkết thúc:

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