\ifcommandkey 正在打印值,尽管键没有值

\ifcommandkey 正在打印值,尽管键没有值

我对乳胶完全是个初学者。

我有一条类似下面的命令,如果键包含值,则应该打印。但是,当我不传递键时coursework,它仍然会打印“相关课程:”,而它本应不打印任何内容。

--test.cls--

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2023/03/18 v0.1 Resume class]
\LoadClass[11pt,letterpaper]{article} % Font size and paper type

% set document margins
\usepackage[left=0.5 in,top=0.4in,right=0.5in,bottom=0.4in]{geometry}
\usepackage[parfill]{parskip} % Remove paragraph indentation
\usepackage{keycommand}
\usepackage{xstring}
\usepackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    urlcolor=black,
}

% Load the fontspec package to set the font
\usepackage[scaled]{helvet} % Load the Helvetica font
\renewcommand\familydefault{\sfdefault} % Set the default font family to sans-serif
\usepackage[T1]{fontenc} % Use the T1 font encoding to ensure proper character rendering

\pagestyle{empty}

\newcommand{\education}{
    \textbf{\MakeUppercase{Education}}
    \vspace{0.4em}
    \hrule
    \vspace{-0.3em}
}

\newkeycommand{\educationItem}[university,college,program,graduation,grade,coursework]{
    {\bfseries \commandkey{program}} \hfill {Graduating \commandkey{graduation}} \\
    {\commandkey{university}} \hfill \ifcommandkey{grade}{\commandkey{grade}}{} \\
    \ifcommandkey{college}{\commandkey{college}}{} \\
    \ifcommandkey{coursework}{ Relevant coursework: \commandkey{coursework}}{}
}

--main.tex--

\documentclass{test} % Use the custom resume.cls style

\begin{document}

\education{
    \educationItem[
        university=Boston University{,} Boston,
        graduation=May 2020,
        grade=3.82 GPA,
        college=The Community College,
        program=B.S.E.{,} Mechanical Engineering,
        coursework=Hardware Design Languages and Programmable Logic{,} Advanced Excel in Business
    ]
    \educationItem[
        university=California University{,} California,
        college=The Honors College,
        graduation=May 2020,
        program=B.S.E.{,} Mechanical Engineering,
    ]
}

\end{document}

输出:

在此处输入图片描述

答案1

\ifcommandkey已损坏(如果再进行一步扩展它实际上就可以工作 - 但似乎包中存在几个错误,缺少一步扩展)。

如果你将以下内容放在你的序言(或课程)中,你就可以修复你的用法\ifcommandkey

\begingroup
\lccode`\_=`\/
\lowercase{%
\endgroup
%% copied from commandkey.sty version 2010/04/27 v3.1415 but altered to make it
%% actually work
\renewcommand*\ifcommandkey[1]{\csname @%
   \expandafter\expandafter\expandafter
   \expandafter\expandafter\expandafter
   \expandafter
   \kcmd@nbk\commandkey{#1}__{first}{second}__%
   oftwo\endcsname}
}

但我建议您使用不同的包(免责声明:我是下面建议的不同包的作者)。

expkv-cs提供了一个类似的界面(不完全相同,但足够相似)。使用它,您不会获得类似 的命令\ifkeycommand,但您可以使用辅助键(您无法通过 访问\ekvcValue但可以更改主键的键)构建比该命令更灵活的东西。在下文中,我使用它通过格式化指令来扩充键值,该指令仅在使用键时才需要。

.cls-file

\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{test}[2023/03/18 v0.1 Resume class]
\LoadClass[11pt,letterpaper]{article} % Font size and paper type

% set document margins
\RequirePackage[left=0.5 in,top=0.4in,right=0.5in,bottom=0.4in]{geometry}
\RequirePackage[parfill]{parskip} % Remove paragraph indentation
\RequirePackage{xstring}

% Load the fontspec package to set the font
\RequirePackage[scaled]{helvet} % Load the Helvetica font
\renewcommand\familydefault{\sfdefault} % Set the default font family to sans-serif
\RequirePackage[T1]{fontenc} % Use the T1 font encoding to ensure proper character rendering

\pagestyle{empty}

\newcommand{\education}{%
    \textbf{\MakeUppercase{Education}}
    \vspace{0.4em}
    \hrule
    \vspace{-0.3em}
}

\RequirePackage{expkv-cs}
\ekvcHash\educationItem
  {%
     university          = {}
    ,program             = {}
    ,graduation          = {}
    ,grade               = {}
    ,college-internal    = {}
    ,coursework-internal = {}
  }
  {%
    \par\noindent
    {\bfseries\ekvcValue{program}{#1}}\hfill Graduating \ekvcValue{graduation}{#1}\\
    \ekvcValue{university}{#1}\hfill\ekvcValue{grade}{#1}% if empty doesn't hurt
    \ekvcValue{college-internal}{#1}%
    \ekvcValue{coursework-internal}{#1}%
  }
\ekvcSecondaryKeys\educationItem
  {%
     meta college    = {college-internal    = {\\#1}}
    ,meta coursework = {coursework-internal = {\\Relevant coursework: #1}}
  }

\RequirePackage{hyperref}
\hypersetup{
    colorlinks=true,
    linkcolor=blue,
    urlcolor=black,
}

.tex-file:

的参数\educationItem现在是一个强制参数(因为它首先实际上不是可选的,并且有了强制参数,事情就会更快更稳定 - 如果您现在想要一个可选参数,您需要将用创建的宏包装在expkv-cs一个宏中,该宏抓取它的可选参数并将其作为强制参数转发)。

\documentclass{test} % Use the custom resume.cls style

\begin{document}

\education{
    \educationItem{
        university=Boston University{,} Boston,
        graduation=May 2020,
        grade=3.82 GPA,
        college=The Community College,
        program=B.S.E.{,} Mechanical Engineering,
        coursework=Hardware Design Languages and Programmable Logic{,} Advanced Excel in Business
    }
    \educationItem{
        university=California University{,} California,
        college=The Honors College,
        graduation=May 2020,
        program=B.S.E.{,} Mechanical Engineering,
    }
}

\end{document}

结果

在此处输入图片描述

相关内容