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 오류: 규모=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}

라텍스로 이것을 시도한 나의 목표는 조건문을 사용하여 새로운 명령을 만드는 데 익숙해지는 것이었습니다. 나는 그 뉘앙스가 그것을 좀 더 어렵게 만들 것이라는 것을 깨닫지 못했습니다. 지금까지 의견을 보내주셔서 감사합니다!

답변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경우에는 축척 비율이 지정되었다고 가정하고 be로 정의합니다. 그렇지 않으면 원래 인수가 에 저장됩니다 .=\picargscale=#1\picarg

여기에 이미지 설명을 입력하세요

이제 문제는: 정말로 그런 매크로를 원하는가? 아니요, 여러 가지 이유가 있습니다.

  1. \pic{0.55}{x}{y}오버 에는 큰 장점이 없습니다 \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}

변형

그러나 내 생각에는 이것은 사용자에게 경멸을 불러일으키는 엉터리 디자인이다. 그러므로 나는 그것을 하지 않을 것이다. 적어도 내가 사용자들 사이에 속하게 된다면 그렇지 않을 것이다. 주행 거리는 항상 그렇듯 다양할 수 있습니다.

관련 정보