使用 \if 时,moderncv \cventry 中出现虚假空格

使用 \if 时,moderncv \cventry 中出现虚假空格

我尝试\cventry使用定义的\ifdetails开关有条件地显示一些信息。但是,由于某种我不知道的原因,创建了一个虚假的空格(或换行符)。

我使用\newtogglefrom测试了这一点etoolbox,结果也是一样。不确定这是否与本身的定义有关\cventry,或者与条件作为宏有关。

另外,请注意 上的奇怪行为\ifdetails。如果我放置%来保护括号后的换行,则需要\protect\ifdetails但是,如果我不放置 ,%则不需要。

有人能解释一下问题是什么吗?我该如何消除虚假的空白?

\documentclass{moderncv}
\moderncvtheme[blue]{classic}

\firstname{Some}
\familyname{Dude}
\title{CV}

\newif\ifdetails
%\detailstrue
\detailsfalse

\begin{document}
\maketitle

\section{Using cventry:}
\cventry{year--year}{THIS IS JUSTIFIED, AS YOU CAN PLAINLY SEE}{THIS IS ALSO JUSTIFIED}{THIS IS JUSTIFIED, TOO}{AND THIS IS JUSTIFIED AS WELL}{
\ifdetails% if not using a % before, I don't need to protect
I would like all this stuff to be raggedright, however. The "year-year" is not important. 
\fi
}
\cventry{year--year}{THIS IS JUSTIFIED, AS YOU CAN PLAINLY SEE}{THIS IS ALSO JUSTIFIED}{THIS IS JUSTIFIED, TOO}{AND THIS IS JUSTIFIED AS WELL}{%<- if using this, I need to \protect below
\protect\ifdetails%
I would like all this stuff to be raggedright, however.
% This more complex content breaks existing solutions
\begin{itemize}
  \item Item 1
  \item Item 2
\end{itemize}
\fi%
}

\end{document}

我得到的是:

在此处输入图片描述

我想要的是:

在此处输入图片描述

编辑:itemize在内容上添加了环境来表明它破坏了解决方案。

答案1

检查空参数的条件moderncv\cventry不合适的。定义如下:

\renewcommand*{\cventry}[7][.25em]{%
  \cvitem[#1]{#2}{%
    {\bfseries#3}%
    \ifthenelse{\equal{#4}{}}{}{, {\slshape#4}}%
    \ifthenelse{\equal{#5}{}}{}{, #5}%
    \ifthenelse{\equal{#6}{}}{}{, #6}%
    .\strut%
    \ifx&#7&%
      \else{\newline{}\begin{minipage}[t]{\linewidth}\small#7\end{minipage}}\fi}}

检查参数是否为空。相反,#7使用来自的测试机制\ifx&#7&%#7对空的扩展宏参数进行安全测试?

在此处输入图片描述

\documentclass{moderncv}
\moderncvtheme[blue]{classic}
\firstname{Some}
\familyname{Dude}
\title{CV}

\newif\ifdetails
%\detailstrue
\detailsfalse

\makeatletter
\renewcommand*{\cventry}[7][.25em]{%
  \cvitem[#1]{#2}{%
    {\bfseries#3}%
    \ifthenelse{\equal{#4}{}}{}{, {\slshape#4}}%
    \ifthenelse{\equal{#5}{}}{}{, #5}%
    \ifthenelse{\equal{#6}{}}{}{, #6}%
    .\strut%
    \begingroup
    \protected@edef\x{\endgroup
      \noexpand\long\noexpand\def\noexpand\argseven{#7}}\x
    \if\relax\detokenize\expandafter{\romannumeral-`\Q\argseven}\relax %\ifx&#7&%
      \else{\newline{}\begin{minipage}[t]{\linewidth}\small#7\end{minipage}}\fi}}
\makeatother

\begin{document}
\maketitle

\section{Using cventry:}
\cventry{year--year}{THIS IS JUSTIFIED, AS YOU CAN PLAINLY SEE}{THIS IS ALSO JUSTIFIED}{THIS IS JUSTIFIED, TOO}{AND THIS IS JUSTIFIED AS WELL}{%
\ifdetails 
I would like all this stuff to be raggedright, however. The "year-year" is not important. 
\fi
}
\cventry{year--year}{THIS IS JUSTIFIED, AS YOU CAN PLAINLY SEE}{THIS IS ALSO JUSTIFIED}{THIS IS JUSTIFIED, TOO}{AND THIS IS JUSTIFIED AS WELL}{
\ifdetails
I would like all this stuff to be raggedright, however. The "year-year" is not important. %
\fi%
}

\end{document}

答案2

第六个强制参数未被评估为空的原因有两个;我将重新格式化代码以便于阅读。

你的代码可以等效地写成

\cventry{year--year}
  {THIS IS JUSTIFIED, AS YOU CAN PLAINLY SEE}
  {THIS IS ALSO JUSTIFIED}
  {THIS IS JUSTIFIED, TOO}
  {AND THIS IS JUSTIFIED AS WELL}
  {
   \ifdetails
     I would like all this stuff to be raggedright, however.
     The "year-year" is not important. 
   \fi
  }

并且第六个参数的左括号后有一个空格。因此,即使忽略条件,参数也不会为空。但条件是不是忽略。

的定义,加上你的输入,就变成\cventry\ifx&#7&

\ifx& \ifdetails...\fi&

因为 TeX 与空格进行比较,所以结果为 false &。但是,使用 来保护行尾并%不能解决问题:使用

\cventry{year--year}
  {THIS IS JUSTIFIED, AS YOU CAN PLAINLY SEE}
  {THIS IS ALSO JUSTIFIED}
  {THIS IS JUSTIFIED, TOO}
  {AND THIS IS JUSTIFIED AS WELL}
  {%
   \ifdetails
     I would like all this stuff to be raggedright, however.
     The "year-year" is not important. 
   \fi
  }

TeX 将执行

\ifx&\ifdetails...\fi&

它会&与进行比较\ifdetails,结果也返回 false。问题是 TeX 在吸收参数时不会扩展标记。

使用设施进行重新定义expl3

\documentclass{moderncv}
\moderncvtheme[blue]{classic}

\usepackage{xparse}
\ExplSyntaxOn
\RenewDocumentCommand{\cventry}{O{.25em}mmmmmm}
 {
  \cvitem[#1]{#2}
   {
    {\bfseries#3}
    \tl_if_blank:nF{#4}{,~{\slshape#4}}
    \tl_if_blank:nF{#5}{,~#5}
    \tl_if_blank:nF{#6}{,~#6}
    .\strut
    \tl_if_blank:fF{#7}
     {
      \newline\begin{minipage}[t]{\linewidth}\small#7\end{minipage}
     }
   }
 }
\cs_generate_variant:Nn \tl_if_blank:nF { f }
\ExplSyntaxOff

\newif\ifdetails
%\detailstrue
\detailsfalse

\firstname{Some}
\familyname{Dude}
\title{CV}

\begin{document}
\maketitle

\section{Using cventry:}

\cventry{year--year}
  {THIS IS JUSTIFIED, AS YOU CAN PLAINLY SEE}
  {THIS IS ALSO JUSTIFIED}
  {THIS IS JUSTIFIED, TOO}
  {AND THIS IS JUSTIFIED AS WELL}
  {\ifdetails 
     I would like all this stuff to be raggedright, however.
     The "year-year" is not important. 
   \fi}

\detailstrue
\cventry{year--year}
  {THIS IS JUSTIFIED, AS YOU CAN PLAINLY SEE}
  {THIS IS ALSO JUSTIFIED}
  {THIS IS JUSTIFIED, TOO}
  {AND THIS IS JUSTIFIED AS WELL}
  {\ifdetails
     I would like all this stuff to be raggedright, however.
     The "year-year" is not important.
   \fi}

\end{document}

我建议您采用这种格式,这样可以更容易地检查参数的数量:六是一个很大的数字,我记得网站上的一些问题恰恰是我忘记了其中的一些。

请注意,\ifdetails应跟在括号后面,或者%如果您喜欢以下格式,则应使用

  {%
   \ifdetails
     I would like all this stuff to be raggedright, however.
     The "year-year" is not important.
   \fi
  }

相关内容