有条件地使用 includegraphics

有条件地使用 includegraphics

我想连续 4 年打印不同的徽标,然后重复使用它们。我的脚本不起作用...有什么想法吗?

\documentclass{article}
\usepackage{pgf,calculator,ifthen}
\MODULO{\year}{4}{\ye}
\newcommand{\autologo}{\ifcase\ye\relax logotp0.pdf\or logotp1.pdf\or logotp2.pdf\else logotp3.pdf\fi}
\newcommand{\auteloge}{\ifcase\ye\relax example-image-a\or example-image-b\or example-image-c\else example-image-d\fi}
\begin{document}
\includegraphics[scale=0.5]{logotp2.pdf}  % works for the 4 image files...
\includegraphics[scale=0.5]{\auteloge}   %works
\includegraphics[scale=0.5]{\autologo}    % error argument of \@firstofone
\end{document}

答案1

文件名中附加了虚假空格:

\documentclass{article}
\usepackage{pgf,calculator,ifthen}
\MODULO{\year}{4}{\ye}
%\newcommand{\autologo}{\ifcase\ye\relax logotp0.pdf \or logotp1.pdf  \or logotp2.pdf  \else logotp3.pdf \fi}
 \newcommand{\autologo}{\ifcase\ye\relax logotp0.pdf\or logotp1.pdf\or logotp2.pdf\else logotp3.pdf\fi}

\begin{document}
\includegraphics[scale=0.5]{logotp2.pdf}  % works...
\includegraphics[scale=0.5]{\autologo}    % but I want something like this
\end{document}

我将您的图像名称替换为example-images 并且它起作用了:

\documentclass{article}
\usepackage{pgf,calculator,ifthen}
\usepackage{duckuments}
\MODULO{\year}{4}{\ye}
\newcommand{\autologo}{%
  \ifcase\ye\relax
      example-image-a%
  \or example-image-b%
  \or example-image-c%
  \else example-image%
  \fi}

\begin{document}
\pagenumbering{gobble}
\includegraphics[scale=1]{example-image-duck}  % works...
\includegraphics[scale=0.5]{\autologo}    % but I want something like this
\end{document}

在此处输入图片描述

TeX 忽略控制序列名称后的空格(例如 、 和 ),但不会忽略普通字符后的空格,因此之前的代码\relax失败\or,因为它找不到名为 的文件。\else\includegraphicslogotp2.pdf .<extension>

编辑:

由于我们显然被困在这里,让我建议使用一种更为强大的方法expl3

\documentclass{article}
\usepackage{pgf,calculator,ifthen}

% Copy this to the preamble of your document
\usepackage{xparse}
\ExplSyntaxOn
\seq_new:N \jom_logos
\NewDocumentCommand\DefineAutologo
  { m }
  {
    \seq_set_from_clist:Nn \jom_logos { #1 }
  }
\NewExpandableDocumentCommand\UseAutologo
  { m }
  {
    \seq_item:Nn \jom_logos { #1 }
  }
\ExplSyntaxOff

% Use this to set the images you want to use
\DefineAutologo{
  example-image-a,
  example-image-b,
  example-image-c,
  example-image
}

\MODULO{\year}{4}{\ye}

\begin{document}
\pagenumbering{gobble}
\includegraphics[scale=0.5]{example-image}  % works...
% Include the image with `\UseAutologo{<image-number>}`
\includegraphics[scale=0.5]{\UseAutologo{\ye}}    % but I want something like this
\end{document}

相关内容