加载以 ltxkey 命名的图像时出错

加载以 ltxkey 命名的图像时出错

我是 TeX 和 LaTeX 的新手,我的第一个项目非常雄心勃勃。计划是使用来ltxkeys定义 D&D 角色扮演游戏的数据库条目,然后使用 LaTeX 模板来漂亮地打印角色表、怪物统计数据等。我刚刚开始,并取得了一些成功,但我遇到了这个测试文件的问题:

\documentclass[letterpaper,landscape]{article}
\pagestyle{empty}
\usepackage[T1]{fontenc}%Use a modern font encoding
\usepackage[sfdefault]{noto}
\usepackage[margin=.5in,nohead,nofoot]{geometry}
\usepackage{graphbox}% Loads graphicx automagically.
\usepackage{anyfontsize}
\usepackage{textpos}
\usepackage[tracingkeys]{ltxkeys}

\setlength{\TPHorizModule}{1in}
\setlength{\TPVertModule}{1in}

\makeatletter  
% Monster Tent Graphic is 300 DPI with:
% * image size of (600x2250 pixels) 2in x 7.5in
% * monster title of 630x62 pixels (2.1in x .207in)
% Use a Data Key: \DnD{WUMPUS}{image}
\newcommand{\DnD}[2]
  {%
    {%
      \def\c{#1}
      \def\v{#2}
      \def\t {\csname DnD@KeyFam@\c \endcsname}%
      \csname cmd\t @\c @\v \endcsname%
    }%
  }

% Define a Creature Data Key:
% \DnDNewCreature{WUMPUS}{title="Grand Wumpus", image=..."}
\newcommand{\DnD@NewCreature}[2]%
  {%
    \ltxkeys@declarekeys[DnD@Creature]{#1}%
      { cmd/\needvalue{title}//;
        cmd/\needvalue{image}//;
      }%
    \expandafter\def\csname DnD@KeyFam@#1\endcsname{DnD@Creature}%
    \ltxkeys@setkeys[DnD@Creature]{#1}{#2}%
  }

\def\gimg {img/MM167-Goblin-BW.png}
\DnD@NewCreature{GoblinBoss}{title=Goblin Boss,image=img/MM167-Goblin-BW.png}

\begin{document}%
\def\m {GoblinBoss}%
\begin{figure}[t]%
  \begin{minipage}{.25\textwidth}%
    % \includegraphics[smash=cc,width=2in,totalheight=2.5in,keepaspectratio,angle=180,hshift=-1.25in,vshift=-2.1in]{\DnD{GoblinBoss}{image}}
    \includegraphics[smash=cc,width=2in,totalheight=2.5in,keepaspectratio,angle=180,hshift=-1.25in,vshift=-2.1in]{img/MM167-Goblin-BW.png}%
    \includegraphics[smash=cr,totalheight=7.5in,width=2.5in]{img/Monster-Tent.png}%
    \begin{textblock}{2.1}(.2,-.42)%
      \rotatebox[origin=c]{180}
        {\centerline{\fontsize{16}{16}\textbf{\DnD{GoblinBoss}{title}}}}  
    \end{textblock}
  \end{minipage}%
\end{figure}
\end{document}
\makeatother

照原样,文件正确呈现了怪物帐篷图像并正确放置了所需的图像和标题,但是,这只是因为该\includegraphics命令使用要加载的文件路径的文字文本,而不是将其从中提取出来ltxkey。如果使用第一个注释掉的\includegraphics命令而不是第二个命令,我会从 includegraphics 内部得到一个模糊的错误,提示有一个"Illegal parameter number in definition of \filename@base"

ltxkey如果我使用而不是,\gimg而将其定义为图像文件路径,则它可以正常工作。如果我尝试将其定义\gimg{\DnD{GoblinBoss}{image}}在此处输入代码,则再次失败。如果我仅打印 的值,则\DnD{GoblinBoss}{image}它看起来是正确的。任何帮助都将不胜感激。

答案1

您需要定义命令,使其扩展为图像文件名。

\documentclass[letterpaper,landscape]{article}
\pagestyle{empty}
\usepackage[T1]{fontenc}%Use a modern font encoding
\usepackage[sfdefault]{noto}
\usepackage[margin=.5in,nohead,nofoot]{geometry}
\usepackage{graphbox}% Loads graphicx automagically.
\usepackage{anyfontsize}
\usepackage{textpos}
\usepackage[tracingkeys]{ltxkeys}

\setlength{\TPHorizModule}{1in}
\setlength{\TPVertModule}{1in}

\makeatletter  
% Monster Tent Graphic is 300 DPI with:
% * image size of (600x2250 pixels) 2in x 7.5in
% * monster title of 630x62 pixels (2.1in x .207in)
% Use a Data Key: \DnD{WUMPUS}{image}
\newcommand{\badDnD}[2]
  {%
    {%non expandable group start
      \def\c{#1} % non expandable assignment and space token
      \def\v{#2} % non expandable assignment and space token
      \def\t {\csname DnD@KeyFam@\c \endcsname}% non expandable assignment
      \csname cmd\t @\c @\v \endcsname%
    }%non expandable group end
  }
\newcommand{\DnD}[2]{\csname cmd\csname DnD@KeyFam@#1\endcsname @#1@#2\endcsname}

% Define a Creature Data Key:
% \DnDNewCreature{WUMPUS}{title="Grand Wumpus", image=..."}
\newcommand{\DnD@NewCreature}[2]%
  {%
    \ltxkeys@declarekeys[DnD@Creature]{#1}%
      { cmd/\needvalue{title}//;
        cmd/\needvalue{image}//;
      }%
    \expandafter\def\csname DnD@KeyFam@#1\endcsname{DnD@Creature}%
    \ltxkeys@setkeys[DnD@Creature]{#1}{#2}%
  }

\def\gimg {img/MM167-Goblin-BW.png}
\DnD@NewCreature{GoblinBoss}{title=Goblin Boss,image=example-image.png}

\begin{document}%
\def\m {GoblinBoss}%
\begin{figure}[t]%
  \begin{minipage}{.25\textwidth}%
     \includegraphics[smash=cc,width=2in,totalheight=2.5in,keepaspectratio,angle=180,hshift=-1.25in,vshift=-2.1in]{\DnD{GoblinBoss}{image}}
    \includegraphics[smash=cc,width=2in,totalheight=2.5in,keepaspectratio,angle=180,hshift=-1.25in,vshift=-2.1in]{example-image-a.png}%
    \includegraphics[smash=cr,totalheight=7.5in,width=2.5in]{example-image-b.png}%
    \begin{textblock}{2.1}(.2,-.42)%
      \rotatebox[origin=c]{180}
        {\centerline{\fontsize{16}{16}\textbf{\DnD{GoblinBoss}{title}}}}  
    \end{textblock}
  \end{minipage}%
\end{figure}
\end{document}
\makeatother this is never executed

在此处输入图片描述

相关内容