ไม่สามารถใช้มาโครภายในวงเล็บหรือสำหรับการตัดแต่ง

Aug 17 2020

ฉันกำลังพยายามกำหนดรูปพารามิเตอร์ทางเลือกเป็นค่าเริ่มต้นด้วยคำสั่งผู้ใช้ แต่ฉันไม่สามารถใช้ภายในไฟล์[ ]. ตัวอย่างถัดไปแสดงข้อยกเว้นเมื่อฉันแทนที่trim=0 0 0 0ด้วย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}

นอกจากนี้วัตถุประสงค์ของฉันคือกำหนดคำสั่งเช่นฉันสามารถแทนที่ทั้งประโยคที่width=\linewidth,trim=0 0 0 0,clipอยู่ข้างใน[ ]ได้

คำตอบ

6 UlrichDiez Aug 17 2020 at 17:02

แก้ไขในวันที่ 20 สิงหาคม 2020

อีกครั้งที่ฉัน (Ulrich Diez) ทำให้ตัวเองตกอยู่ในสถานการณ์ที่น่าอับอาย:

ในขณะที่เขียนสิ่งต่างๆด้านล่างฉัน (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นำไปสู่การที่จะขยาย -chain \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}%%%%%

สิ้นสุดการแก้ไขในวันที่ 20 สิงหาคม 2563



หากคำจำกัดความของ\trim-command สามารถเปลี่ยนจากรูปเป็นรูปได้คุณสามารถเช่นใช้\romannumeral0เทคนิค -expansion- และอาร์กิวเมนต์ - แลกเปลี่ยน - ส่วนสำคัญของ\romannumeral0-expansion คือ:

  • TeX ขยายโทเค็นที่ขยายได้ในขณะที่รวบรวมโทเค็นที่เป็นของ⟨number⟩ -ปริมาณที่จะแสดงเป็นตัวเลขโรมัน
  • หากโทเค็นแรกที่ TeX พบในขณะที่รวบรวม⟩number⟩ -quantity เป็นตัวเลขเช่น0กระบวนการรวบรวมโทเค็นที่เป็นของ⟨number⟩ -quantity จะกลายเป็นกระบวนการรวบรวมตัวเลขที่มากขึ้นหรือสิ่งที่ไม่ใช่ ตัวเลขจึงยุติกระบวนการรวบรวม โทเค็นที่ขยายได้จะขยายในขณะที่รวบรวมตัวเลข โทเค็นเว้นวรรคที่ยุติลำดับตัวเลขจะยุติกระบวนการรวบรวมตัวเลขจำนวนมากขึ้นและถูกทิ้งโดยไม่โต้ตอบ
  • หากจำนวนที่รวบรวมไม่เป็นบวก 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ทริกเกอร์กระบวนการส่งมอบการขยายระดับบนสุดของรายการถัดไป แต่หนึ่งโทเค็นและยุติเมื่อกระบวนการได้รับส่วนขยายระดับบนสุดของรายการถัดไป แต่โทเค็นหนึ่งสิ้นสุดลง . ดังนั้น:

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

โทเค็นถัดไป แต่เป็นโทเค็นที่ส่งออกจากอันแรก\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}%%%%%

โทเค็นถัดไป แต่เป็นโทเค็นที่ส่งออกจากที่สอง\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⟩จะกลายเป็นกระบวนการรวบรวมตัวเลขมากขึ้นหรือสิ่งที่ยุติลำดับตัวเลข:

% 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 พบรายการที่สาม\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}%%%%%

โทเค็นถัดไป แต่เป็นโทเค็นหนึ่งที่ส่งออกจากที่สาม\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}%%%%%

ถัดไป แต่อย่างหนึ่งที่ส่งออกจากโทเค็นที่สี่\expandafterมี\trimดังนั้นกระบวนการของการได้รับระดับบนสุดขยายตัวของคนที่สี่\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}%%%%%

เมื่อกระบวนการได้รับส่วนขยายระดับบนสุดของหน่วยที่สี่\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
  %     terminated.
  \exchange
  {0 0 0 0}{ width=\linewidth,trim=},clip%
]{/path/to/graphics/file.jpg}%%%%%

เมื่อกระบวนการได้รับส่วนขยายระดับบนสุดของส่วนที่สาม\expandafterสิ้นสุดลง- การขยายจะ\romannumeralดำเนินต่อไปซึ่งจะทำให้เกิดการขยายตัว\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 ค้นหา Space-token Space-token นั้นยุติกระบวนการย่อยในการรวบรวมตัวเลขจำนวนมากขึ้นของจำนวน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 terminated; number "0" found.
  width=\linewidth,trim=0 0 0 0,clip%
]{/path/to/graphics/file.jpg}%%%%%

ในฐานะที่เป็นเท็กซ์พบ⟨number⟩ -quantity ที่มีค่า0ในขณะที่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 terminated.
  width=\linewidth,trim=0 0 0 0,clip%
]{/path/to/graphics/file.jpg}%%%%%

เมื่อกระบวนการได้รับส่วนขยายระดับบนสุด\romannumeralสิ้นสุดลงกระบวนการรับส่วนขยายระดับบนสุดของครั้งที่สอง\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}%%%%%

เมื่อกระบวนการได้รับส่วนขยายระดับบนสุดของวินาที\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}%%%%%