# How can I use versions.sty to change a section header?

I am trying to have different section headers in different versions of a document using versions.sty (0.55). I get and error while compiling; here is a MWE :

%!TEX encoding = UTF-8 Unicode
%!TEX TS-program = pdflatexmk
\documentclass{article}

\usepackage{versions}
\includeversion{draft}

\begin{document}
\section{\begin{draft}This is a draft of\end{draft} Introduction}
Some text
\end{document}


The error is :

Argument of \@sect has an extra }.
<inserted text>
\par
l.9 ...This is a draft of\end{draft} Introduction}


Any suggestion? Thanks!

• I suspect \section{\protect\begin{draft}This is a draft of\protect\end{draft} Introduction} – user31729 Sep 21 '17 at 17:08
• @ChristianHupfer Thanks, but that does not work here. (I always try \protect and \expandafter...) – A.G. Sep 21 '17 at 17:15
• Compilation worked for me with \protect ... – user31729 Sep 21 '17 at 17:17
• @ChristianHupfer My wrong, I missed the second \protect. It works with \includeversion but I get an error if I \excludeversion. – A.G. Sep 21 '17 at 17:25

Using \section{\protect\begin{draft}This is a draft of\protect\end{draft} Introduction} works for the headline and the ToC, but not for the page header.

The versions package also offeres the macro \processifversion, which can be used here. It works for all three, head line, ToC, and page header.

Edit:

The space between of and Introduction must be part of the optional content. Otherwise it will appear in the headline, if \excludeversion is used. And \protect can't be used, because with it, This is a draft of will appear in the heading regardless of \excludeversion. This doesn't look nice in the source, but the space logicaly belongs to the draft content.

\documentclass{article}

\usepackage{versions}
\includeversion{draft}

\begin{document}
\tableofcontents

\newpage
\section{\protect\processifversion{draft}{This is a draft of }Introduction}
% watch the  space -----------------------------------------^
Some text
\end{document}

• Thanks, this works great. However the \protect solution generates an error if I say \excludeversion{draft}. – A.G. Sep 21 '17 at 23:04
• @A.G. I don't get an error here, but the heading still contains This is a draft of. Is that what you mean? – Mike Sep 21 '17 at 23:45
• I get a compile error and entering scrollmode produces the output with this is a draft of. The error message is Extra }, or forgotten \endgroup. \@sect ...@svsec }\interlinepenalty \@M #8\@@par } \endgroup \csname #1mark\e... l.10 ... draft of\protect\end{draft} Introduction}. But your solution works much better anyways. – A.G. Sep 21 '17 at 23:49
• @A.G. ah, the error is produced by the version with the draft environment. – Mike Sep 21 '17 at 23:56
• @A.G. I changed my answer. It should now work for headline, ToC, and header with both \includeversion and \excludeversion. – Mike Sep 22 '17 at 0:11