
画像のサイズを決めるために、スケール サイズまたは異なるスケーリング パラメータ (例: 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
場合は、スケール係数が指定されていると仮定して、 を と定義します。 それ以外の場合は、元の引数が に格納されます。=
\picarg
scale=#1
\picarg
ここで問題になるのは、本当にそのようなマクロが必要かどうかです。いいえ、いくつかの理由があります。
\pic{0.55}{x}{y}
を超えることに大きな利点はありません\pic{scale=0.55}{x}{y}
。scale
は間違ったキーです。画像のサイズも、ドキュメントの最終的なサイズもわからない可能性があります。テキストの幅を少し変更するだけで、複数の画像のサイズを変更する必要があります。figure
に隠れても利点はありません\pic
。1 行に収まる 2 枚の画像がある場合は、それらを並べて配置するとよいかもしれません。この
[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
via を展開することで修正されます。\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}
これにより、次の 2 つのページが生成されます。
答え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}
しかし、これは私の意見では、ユーザーを軽蔑する不器用なデザインです。したがって、少なくとも私がユーザーになるつもりなら、私はこれをやりません。走行距離は、いつものように、異なる可能性があります。