无法在我的乳胶代码中发现错误

无法在我的乳胶代码中发现错误

你好,LaTeX 社区,

我正在制作一个模板,几乎要完成了,但我的代码中有一个故障,我无法找到它。在渲染代码时,无法\textbf{Question \questionnumber:} \questiontext 正确渲染。\questionnumber输出中未显示。

当前输出:Question : 期望输出:Question 1 :

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{xkeyval}
\usepackage{ragged2e}
\usepackage{float}
\usepackage{caption}

\geometry{top=2.5cm, bottom=1.25cm, left=1.5cm, right=1.5cm}

\makeatletter

% DEFINE KEY FOR mcqimgsideFourOne %

\makeatletter
\define@key{mcqimgsideFourOne}{questionnumber}{\def\questionnumber{#1}}
\define@key{mcqimgsideFourOne}{questiontext}{\def\questiontext{#1}}
\define@key{mcqimgsideFourOne}{imgwidth}{\def\imgwidth{#1}}
\define@key{mcqimgsideFourOne}{imgheight}{\def\imgheight{#1}}
\define@key{mcqimgsideFourOne}{img}{\def\img{#1}}
\define@key{mcqimgsideFourOne}{imgCaption}{\def\imgCaption{#1}}
\define@key{mcqimgsideFourOne}{optionA}{\def\optionA{#1}}
\define@key{mcqimgsideFourOne}{optionB}{\def\optionB{#1}}
\define@key{mcqimgsideFourOne}{optionC}{\def\optionC{#1}}
\define@key{mcqimgsideFourOne}{optionD}{\def\optionD{#1}}
\define@key{mcqimgsideFourOne}{author}{\def\author{#1}}
\define@key{mcqimgsideFourOne}{slot}{\def\slot{#1}}
\define@key{mcqimgsideFourOne}{leftmini}{\def\leftmini{#1}}
\define@key{mcqimgsideFourOne}{rightmini}{\def\rightmini{#1}}
\makeatother

\newcommand{\mcqimgsideFourOne}[1]{
  \setkeys{mcqimgsideFourOne}{
    questionnumber =,
    questiontext =,
    imgwidth =,
    imgheight =,
    img =,
    imgCaption =,
    optionA =,
    optionB =,
    optionC =,
    optionD =,
    author =,
    slot =,
    leftmini =, 
    rightmini =, 
    #1
  }

  \vspace{2.5mm}
  \begin{raggedright}
    \textbf{Author:} \author \hfill \textbf{Slot:} \slot\\
  \end{raggedright}
  \vspace{\baselineskip}

  \noindent
  \begin{minipage}[t]{\leftmini\textwidth}
    \textbf{Question \questionnumber:} \questiontext
      \vspace{2mm} \\
      (a) \medskip \optionA\\
      (b) \medskip \optionB\\
      (c) \medskip \optionC\\
      (d) \medskip \optionD\\
  \end{minipage}%
  \begin{minipage}[t]{\rightmini\textwidth}
    \vspace{-\baselineskip}
    \centering
    \includegraphics[width=\imgwidth, height=\imgheight]{\img}
    \captionsetup{type=figure, labelformat=empty} 
    \caption{\imgCaption} 
    \label{fig:sample}
  \end{minipage}
}

%-----------------------------------------------------------
%                        Start Question
%-----------------------------------------------------------
\begin{document}

%-----------------------------------------------------------
%                        Question [ 1 ]
%-----------------------------------------------------------
% start-of-question
\mcqimgsideFourOne{
  questionnumber={1}, 
  questiontext={Which of the following is an example of a mammal?},
  imgwidth={3cm},
  imgheight={3cm},
  img={example-image-a},
  imgCaption={A sample image of a mammal},
  optionA={Fish},
  optionB={Bird},
  optionC={Dog},
  optionD={Insect},
  author={Author 1},
  slot={C},
  leftmini={0.6},
  rightmini={0.4},
} 
% end-of-question  

\end{document}

注意:我是 LaTeX 和这个社区的新手,如果您有任何建议或与我当前方法不同的方法/想法,请随时评论您的想法和想法,这将有助于我提高代码质量。

答案1

问题是您在使用时提供的第一个键前面添加了两个空格。第一个是由 的错误定义添加的\mcqimgsideFourOne,第二个是由它的使用添加的。使用时的空格不应该是一个问题,如果定义正确,它也不会是一个问题。但是xkeyval(与大多数其他 key=value 解析器一样)只会从键或值的两端删除一个空格。因此会搜索␣questionnumber未定义的。

如果你修复了你的定义,你的使用就会有效(请注意,这个答案只能修复错误,并不能改善其他任何东西):

\documentclass[12pt, a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{geometry}
\usepackage{graphicx}
\usepackage{xkeyval}
\usepackage{ragged2e}
\usepackage{float}
\usepackage{caption}

\geometry{top=2.5cm, bottom=1.25cm, left=1.5cm, right=1.5cm}

\makeatletter

% DEFINE KEY FOR mcqimgsideFourOne %

\makeatletter
\define@key{mcqimgsideFourOne}{questionnumber}{\def\questionnumber{#1}}
\define@key{mcqimgsideFourOne}{questiontext}{\def\questiontext{#1}}
\define@key{mcqimgsideFourOne}{imgwidth}{\def\imgwidth{#1}}
\define@key{mcqimgsideFourOne}{imgheight}{\def\imgheight{#1}}
\define@key{mcqimgsideFourOne}{img}{\def\img{#1}}
\define@key{mcqimgsideFourOne}{imgCaption}{\def\imgCaption{#1}}
\define@key{mcqimgsideFourOne}{optionA}{\def\optionA{#1}}
\define@key{mcqimgsideFourOne}{optionB}{\def\optionB{#1}}
\define@key{mcqimgsideFourOne}{optionC}{\def\optionC{#1}}
\define@key{mcqimgsideFourOne}{optionD}{\def\optionD{#1}}
\define@key{mcqimgsideFourOne}{author}{\def\author{#1}}
\define@key{mcqimgsideFourOne}{slot}{\def\slot{#1}}
\define@key{mcqimgsideFourOne}{leftmini}{\def\leftmini{#1}}
\define@key{mcqimgsideFourOne}{rightmini}{\def\rightmini{#1}}
\makeatother

\newcommand{\mcqimgsideFourOne}[1]{
  \setkeys{mcqimgsideFourOne}{
    questionnumber =,
    questiontext =,
    imgwidth =,
    imgheight =,
    img =,
    imgCaption =,
    optionA =,
    optionB =,
    optionC =,
    optionD =,
    author =,
    slot =,
    leftmini =,
    rightmini =,% THIS LINE CHANGED
    #1
  }

  \vspace{2.5mm}
  \begin{raggedright}
    \textbf{Author:} \author \hfill \textbf{Slot:} \slot\\
  \end{raggedright}
  \vspace{\baselineskip}

  \noindent
  \begin{minipage}[t]{\leftmini\textwidth}
    \textbf{Question \questionnumber:} \questiontext
      \vspace{2mm} \\
      (a) \medskip \optionA\\
      (b) \medskip \optionB\\
      (c) \medskip \optionC\\
      (d) \medskip \optionD\\
  \end{minipage}%
  \begin{minipage}[t]{\rightmini\textwidth}
    \vspace{-\baselineskip}
    \centering
    \includegraphics[width=\imgwidth, height=\imgheight]{\img}
    \captionsetup{type=figure, labelformat=empty}
    \caption{\imgCaption}
    \label{fig:sample}
  \end{minipage}
}

%-----------------------------------------------------------
%                        Start Question
%-----------------------------------------------------------
\begin{document}

%-----------------------------------------------------------
%                        Question [ 1 ]
%-----------------------------------------------------------
% start-of-question
\mcqimgsideFourOne{
  questionnumber={1},
  questiontext={Which of the following is an example of a mammal?},
  imgwidth={3cm},
  imgheight={3cm},
  img={example-image-a},
  imgCaption={A sample image of a mammal},
  optionA={Fish},
  optionB={Bird},
  optionC={Dog},
  optionD={Insect},
  author={Author 1},
  slot={C},
  leftmini={0.6},
  rightmini={0.4},
}
% end-of-question

\end{document}

相关内容