有關 xparse \newcommand 的協助

有關 xparse \newcommand 的協助

我有一些圖像需要剪切,還有一些需要剪切。我使用兩個\newcommand有效的解決方案實現了一個解決方案,但我使用 xparse 尋找一個不起作用的緊湊解決方案,我不明白為什麼。

我將不勝感激任何幫助糾正和改進我的下面的程式碼。

\documentclass[11pt]{article}

\usepackage{easyfig}

% [cut up]{file}{caption}{label}{scale} - Cut up % <== Ok
\newcommand{\ruleone}[5][.05]{%
    \Figure[trim={.0\width} {.0\height} {.0\width} {#1\height},clip,%
        width=#5\linewidth,keepaspectratio=true,%
        caption={#3},label={#4},center,here]{#2}
}

% [cut down]{file}{caption}{label}{scale} - Cut down % <== Ok
\newcommand{\ruletwo}[5][.05]{%
    \Figure[trim={.0\width} {#1\height} {.0\width} {.0\height},clip,%
        width=#5\linewidth,keepaspectratio=true,%
        caption={#3},label={#4},center,here]{#2}
}

% [cut up]{file}[cut down]{caption}{label}[scale] - Cut down % <== compile with errors
\newcommand{\onerule}[ o m o m m o ]{%
    \Figure[trim={.0\width} {ifNoValueTF{#1}{.0}{#1}\height}%
        {.0\width} {ifNoValueTF{#3}{.0}{#3}\height},clip,%
        width={ifNoValueTF{#6}{1}{#6}\linewidth},keepaspectratio=true,%
        caption={#4},label={#5},center,here]{#2}
}

\begin{document}

Here comes the images:

\ruleone[.45]{example-image-a}{First image}{label1}{.5} % <== this works

\ruletwo[.35]{example-image-a}{Second image}{label2}{.5} % <== This too

\onerule[.35]{example-image-b}{Third image}{label3}{.5}

\onerule{example-image-b}[0.45]{Third image}{label3}{.5}

\end{document}

答案1

在 中xparse,定義新指令的指令是\NewDocumentCommand而不是\newcommand。此外,描述參數的第二個參數是強制性的({...}而不是[...])並且ifNoValueTF應該是\IfNoValueTF(大寫並帶有反斜線)。

此外,您%在帶有修剪參數的行末尾會抑制所需的空間,並且比例參數是可選的([...]而不是{...})。

然後你得到

\documentclass[11pt]{article}

\usepackage{xparse,easyfig}

% [cut up]{file}{caption}{label}{scale} - Cut up % <== Ok
\newcommand{\ruleone}[5][.05]{%
    \Figure[trim={.0\width} {.0\height} {.0\width} {#1\height},clip,%
        width=#5\linewidth,keepaspectratio=true,%
        caption={#3},label={#4},center,here]{#2}
}

% [cut down]{file}{caption}{label}{scale} - Cut down % <== Ok
\newcommand{\ruletwo}[5][.05]{%
    \Figure[trim={.0\width} {#1\height} {.0\width} {.0\height},clip,%
        width=#5\linewidth,keepaspectratio=true,%
        caption={#3},label={#4},center,here]{#2}
}

% [cut up]{file}[cut down]{caption}{label}[scale] - Cut down % <== compile with errors
\NewDocumentCommand \onerule { o m o m m o }{%
    \Figure[trim={.0\width} {\IfNoValueTF{#1}{.0}{#1}\height}
        {.0\width} {\IfNoValueTF{#3}{.0}{#3}\height},clip,%
        width={\IfNoValueTF{#6}{1}{#6}\linewidth},keepaspectratio=true,%
        caption={#4},label={#5},center,here]{#2}
}

\begin{document}

Here comes the images:

\ruleone[.45]{example-image-a}{First image}{label1}{.5} % <== this works

\ruletwo[.35]{example-image-a}{Second image}{label2}{.5} % <== This too

\onerule[.35]{example-image-b}{Third image}{label3}[.5]

\onerule{example-image-b}[0.45]{Third image}{label3}[.5]

\end{document}

相關內容