# Spurious space on moderncv \cventry using \if

I am trying to display some information in a \cventry conditionally using a defined \ifdetails switch. However, for some reason that is beyond my knowledge, an spurious space (or new line) is created.

I tested this using \newtoggle from etoolbox and the same happens. Not sure if it has to do with the definition of the \cventry itself, or with the conditionals as macros.

Also, notice the strange behavior on the \ifdetails. If I put a % to protect the change of line after the brace, I need to \protect the \ifdetails. However, if I don't put that % it is not required.

Can someone explain what is the problem? And how can I eliminate the spurious white space?

\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}

What I get:

What I want:

Edit: I added the itemize environment on the contents to show that it breaks solutions.

The condition for checking an empty argument within moderncv's \cventry is not appropriate. Here's the definition:

\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}}

The check on argument #7 uses \ifx&#7&% to see whether #7 is empty. Instead, use a test mechanism from Safe test for an empty expanded macro argument?:

\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}
• Sorry to revive this question, but I just noticed that when using \detailstrue and the % after the opening brace, the code doesn't work. And if one does not uses the % then a spurious space appears. Any thoughts?
Jul 23, 2015 at 17:39
• @adn: With \detailstrue and no % after the opening brace (not in the definition, but in the \cventry use), I don't get a spurious space.
– Werner
Jul 23, 2015 at 17:54
• I get this error: "! Incomplete \if; all text was ignored after line 28. <inserted text> \fi"
Jul 23, 2015 at 17:56
• @adn: Here's a MWE that doesn't provide any problems, so you see what I mean.
– Werner
Jul 23, 2015 at 17:58
• @adn: Yes, but I don't have a solution. My suggestion would be to minimize the problem (since it's probably unrelated to moderncv) and ask a new question.
– Werner
Jul 29, 2015 at 16:58

There are two reasons why the sixth mandatory argument is not evaluated to empty; I'll reformat the code for better reading.

Your code can be equivalently written

\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
}

and there is a space after the opening brace of the sixth argument. So, even if the conditional was ignored, the argument would not be empty. But the conditional is not ignored.

The definition of \cventry does \ifx&#7& and with your input this becomes

\ifx& \ifdetails...\fi&

which is evaluated to false, because TeX compares & with a space. However, protecting the end-of-line with % would not solve the problem: with

\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 would execute

\ifx&\ifdetails...\fi&

and it would compare & with \ifdetails, which returns false as well. The problem is that TeX does not expand tokens when it is absorbing arguments.

A possible redefinition, using expl3 facilities, is

\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}

I suggest you to adopt this format, that makes it easier to check the number of arguments: six is a large number and I remember some questions on the site where the issue was precisely having forgotten some of them.

Note that \ifdetails should follow the brace or % should be used if you prefer a format such as

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