有條件地解析 newcommand 輸入會導致“包 keyval 錯誤:比例未定義”

有條件地解析 newcommand 輸入會導致“包 keyval 錯誤:比例未定義”

我正在嘗試製作一個新的宏,它採用比例大小或不同的縮放參數(例如 width=\textwidth)來調整圖片大小。這是我所擁有的:

\newcommand{\pic}[3]{\begin{figure}[H]
    \centering
    \if\instring{#1}{=}\def \arg{#1}\else\def\arg{scale=#1}\fi
    \includegraphics[\arg]{#2}
    \caption{#3}
\end{figure}}

後來在我的文件中,我有以下行:

\pic{1}{lpm_block.png}{Block diagram for the LPM multiplier}

這給了錯誤「包 keyval 錯誤:scale=1 未定義」。如果沒有「if」語句,它就可以正常運作。我需要改變什麼?有一個更好的方法嗎?謝謝!

編輯:

這是更多應該編譯沒有問題的程式碼。

\documentclass{article}

\usepackage{siunitx}
\usepackage{graphicx}
\usepackage{apacite}
\usepackage{amsmath} % Required for some math elements
\usepackage{multicol} % multiple columns
\usepackage{float} % place graphics
\usepackage{subfig} % graphics on same line
\usepackage{geometry} % fill empty margins
\usepackage[square,sort,comma,numbers]{natbib}
\usepackage[hyphens]{url}
\usepackage{minted}
\renewcommand\theFancyVerbLine{\normalsize\arabic{FancyVerbLine}}

\geometry{letterpaper, top=0.75in, left=0.75in, bottom=0.75in, right=0.75in}

\setlength\parindent{0pt} % Removes all indentation from paragraphs

%\usepackage{times} % Uncomment to use the Times New Roman font

\newcommand{\pic}[3]{\begin{figure}[H]
    %\if\instring{#1}{=}\def \arg{#1}\else\def\arg{scale=#1}\fi
    \centering
    \includegraphics[scale=#1]{#2}
    \caption{#3}
\end{figure}}

\begin{document}

\section{Design Files}
    \subsection{Serial Multiplier}
        \pic{0.55}{serial_block.png}{Block diagram for the serial multiplier. high9 and low8 are simply registers, so they do not have separate design files}

\end{document}

我嘗試使用 Latex 的目的是提高對使用條件語句建立新指令的熟悉程度。我沒有意識到細微差別會讓事情變得更加困難。感謝您迄今為止的投入!

答案1

正如你所說,我把這當作一次練習。稍後評論。

您想要的是決定是否=出現在第一個參數。

\documentclass{article}
\usepackage{graphicx}

\newcommand{\pic}[3]{%
  \begin{figure}
  \centering
  \checkequals{#1}%
  \expandafter\includegraphics\expandafter[\picarg]{#2}
  \caption{#3}
  \end{figure}%
}
\makeatletter
\newcommand\checkequals[1]{\check@equals{#1}#1=\@nil}
\def\check@equals#1#2=#3\@nil{%
  \if\relax\detokenize{#3}\relax
    % no =
    \def\picarg{scale=#2}%
  \else
    \def\picarg{#1}%
  \fi
}
\makeatother

\begin{document}

\pic{0.55}{example-image}{First example}

\pic{scale=0.55}{example-image}{Second example}

\pic{width=2cm,height=1cm}{example-image}{Third example}

\end{document}

巨集調用帶有分隔參數的\checkequals輔助函數;\check@equals如果=沒有出現,我們定義\picargscale=#1,假設指定了比例因子。否則,原始參數儲存在 中\picarg

在此輸入影像描述

現在的問題是:你真的想要這樣的宏嗎?不,有幾個原因。

  1. \pic{0.55}{x}{y}over並沒有什麼太大的優勢\pic{scale=0.55}{x}{y}

  2. scale是使用錯誤的鍵,因為您可能不知道圖片的大小,也不知道文件的最終大小。文字寬度的微小變化將迫使您重新縮放幾張圖片。

  3. figure躲在裡面沒有任何好處\pic;如果您有兩張可以排成一條線的圖片,您可能需要將它們並排放置。

  4. [H]選項是破壞文件的完美選擇。如果一個人影必須留下就在那裡,沒有其他地方,那麼它就不需要標題,因為它的描述無論如何都會在它旁邊。使用浮動有助於更好地組織材料,以獲得良好的分頁效果。

最後評論。你的程式碼

%\usepackage{times} % Uncomment to use the Times New Roman font

是在說謊。該times包只會改變文字字體,而不是數學字體。如果您確實想使用 Times,請執行下列操作

\usepackage{newtxtext}
\usepackage{newtxmath}% with possible option, see the manual for newtx

答案2

套件keyval使用逗號,作為鍵值對的分隔符,並使用等號=來分隔鍵和值。它不擴展巨集。因此,這些語法字元一定不能隱藏在巨集中。

問題中的範例是透過在處理其可選參數之前擴展來\arg修復的\expandafter\includegraphics

\if\instring{#1}{=}\def \arg{#1}\else\def\arg{scale=#1}\fi
\expandafter\includegraphics\expandafter[\arg]{#2}%

答案3

如果您使用預設的可選參數,scale=1則無需檢查它是否為空:

\documentclass{article}
\usepackage{mwe}

\newcommand{\pic}[3][scale=1]{%
  \begin{figure}
    \centering
    \includegraphics[#1]{#2}
    \caption{#3}
  \end{figure}%
}

\begin{document}

   \pic{example-image-1x1}{Example image}

   \pic[width=\textwidth]{example-image-1x1}{Wider image}
\end{document}

這會產生以下兩個頁面:

在此輸入影像描述

答案4

請注意,我認為這根本不明智,我建議重新考慮您的需求。但是,如果我想肆意破壞,我會使用LaTeX 3來破壞它。但如果您剛學習 LaTeX 2e,這可能不是最好的起點。因此,如果您必須這樣做,那麼您可能會嘗試這樣做的一種方法如下。

\documentclass{article}
\usepackage{graphicx,caption}
\makeatletter
\def\pict@aux#1=#2\@nil{#2}
\newcommand{\pict}[3]{%
  \begin{figure}
    \centering
    \edef\tempa{\expandafter\pict@aux#1=\@nil}%
    \edef\tempb{}%
    \ifx\tempa\tempb
    \includegraphics[scale=#1]{#2}%
    \else
    \includegraphics[#1]{#2}%
    \fi
    \captionof{figure}{#3}%
  \end{figure}%
}
\makeatother

\begin{document}

\section{Design Files}
\subsection{Serial Multiplier}
\pict{0.55}{\jobname-cx}{Block diagram for the serial multiplier. high9 and low8 are simply registers, so they do not have separate design files}
\pict{width=0.55cm}{\jobname-cx}{Block diagram for the serial multiplier. high9 and low8 are simply registers, so they do not have separate design files}
\end{document}

變化

然而,在我看來,這是一種拙劣的設計,對使用者充滿蔑視。因此,我不會這樣做 - 至少,如果我要成為用戶之一,我不會這樣做。您的公里數可能一如既往地有所不同。

相關內容