2

Is there an easy way to let LaTeX ignore parts of the argument to a \section command when writing the .aux file?

Example:

\documentclass[halfparskip]{scrartcl}
\usepackage{lastpage}

\newcommand*{\points}[1]{%
    \noindent%
    \raggedright%
    {\nobreak\hfill\penalty50\hskip1em\null\nobreak%
     \hfill#1 Points%
     \parfillskip=0pt \finalhyphendemerits=0 \par}%
}

\renewcommand{\thesection}{Question \arabic{section}:}

\begin{document}

\section{Testing a test \points{4}}

Lorem Ipsum and so on. This is just a longer text that is meant 
to fill the space. The actual text is not relevant for this question.

\end{document}

With the lastpage package loaded, compilation fails because of the \par in the .aux file generated:

(./minimal.aux
Runaway argument?
{{1}{1}{Testing a test \noindent \protect \raggedright {\protect \nobreak \ETC.
! Paragraph ended before \@newl@bel was complete.
<to be read again> 
                   \par 
l.4 ...rfillskip =0pt \finalhyphendemerits =0 \par
                                                   }}{}{}}

This problem could be fixed by modifying the macro "points" to avoid writing "\par" and perhaps a few other commands to the aux file. The "\par" command is there to fix that with the "halfparskip" option of Scrartcl, the "\hfill" does not fill all the way to the end, which is a suggested solution at Trouble with \hfill not going to the end of the page. This admittedly unclean solution also works when not including the "lastpage" package, which is however needed in the actual document of interest (and not this minimal non-working example).

I am aware that defining an alternative command to "\section", receiving two parameters, of which only one will go into the outline, is probably the cleanest way, but they may be harder to maintain after changes to other packages if there is a more simple way to ignore "3 points" when writing the .aux file.

5
  • Testing a test\texorpdfstring{\hfill 3points}{}
    – egreg
    Commented Aug 15 at 8:38
  • relevant is here not what is written to the aux file, but the out file. Commented Aug 15 at 8:47
  • @UlrikeFischer Thanks for your comment. I realized that the simplified question that I originally put to avoid the LaTeX code being too long was too simplified. I've updated the minimal non-working example to capture more precisely what the problem is.
    – DCTLib
    Commented Aug 15 at 8:57
  • @egreg Thanks for your comment. I realized that I had to make the question more precise regarding the actual goal of the exercise, so I updated it.
    – DCTLib
    Commented Aug 15 at 9:01
  • 1
    you could make the definition robust with \NewDocumentCommand{\points}{m}{%. Commented Aug 15 at 9:11

2 Answers 2

4

When you use a command like your \point in a sectioning command it can reappear in five places: The section title, the page header, the table of contents, the outline, in name references. That means that you carefully need to consider what it means in all these places. It is possible to suppress it various places, but in your case imho it would be better to redefine the sectioning command so that it e.g. takes an optional argument for the points and adds that to the title. Then you wouldn't have to suppress them everywhere else.

\documentclass{scrartcl}
\usepackage[paperheight=5cm,top=2cm,]{geometry}

\makeatletter
\newcommand*{\pointsA}[1]{pointsA #1}
\newcommand*{\pointsB}[1]{\texorpdfstring{\ifx\protect\@unexpandable@protect \else pointsB #1\fi}{}}
\makeatother
\usepackage{hyperref}
\GetTitleStringSetup{expand}

\begin{document}
\tableofcontents


\pagestyle{headings}
\newpage
\section{Testing a test \pointsA{4} \pointsB{5}}\label{sec}

A reference: \nameref{sec} Lorem Ipsum and so on. 

\end{document}

screenshot

3

\par is wrong in that positition; the \section code will take care of that. Also \noindent and \raggedright are useless.

Add a suitable \texorpdfstring command:

\documentclass{scrartcl}
\usepackage{lastpage}
\usepackage{hyperref}
\usepackage{bookmark}

\newcommand*{\points}[1]{%
  \texorpdfstring{\unskip
    {\nobreak\hfill\penalty50\hskip1em\null\nobreak
     \hfill\mbox{#1 Points}%
     \parfillskip=0pt \finalhyphendemerits=0 }% 
  }{}%
}

\begin{document}

\section{Testing a test \points{4}}

Lorem Ipsum and so on. This is just a longer text that is meant 
to fill the space. The actual text is not relevant for this question.

\section{Testing a test with a long title that breaks across lines\points{4}}

Lorem Ipsum and so on. This is just a longer text that is meant 
to fill the space. The actual text is not relevant for this question.

\end{document}

output

6
  • I appreciate the effort. The thing is that what i posted above is just the minimal example showing my problem. What I want to fix is with a series of documents having multiple more complex macros and that include 10s of packages, where the \par actually has an effect on the layout (see for instance tex.stackexchange.com/questions/628641/… where a \par was used to fix hfill's behavior) due to...unknown reasons,. I can either most the original document or spend many many hours on stripping it down to a version that does show ...
    – DCTLib
    Commented Aug 15 at 10:22
  • the very precise problem, which may be left unanswered here. So I wrote this minimal example showing the goal to be achieved. That the effect can be achieved in a different way in this particular case is of course correct, but the question still makes sense, I believe. The document of interest also worked with the previous version of TeXLive, but not with the current one - hence the hope that there is a quick fix to the problem observed, namely that pdflatex complains about the "\par" in the aux file, which doesn't have to be there.
    – DCTLib
    Commented Aug 15 at 10:25
  • @DCTLib I'm not sure where the problem is: you have a definition of \points which has been wrong all the time and worked by chance. Fix it as suggested and carry on.
    – egreg
    Commented Aug 15 at 10:39
  • @egrep I've found the interaction causing the problem: the "halfparskip" option of scrarctl was missing. With it, the "\par" has an effect in the original document and this is why the "\par" was in the \points macro to being with - which would actually work unless the "lastpage" package is loaded. If you have an alternative proposal for making \hfill work correctly in section headings when using the "halfparskip" option of Scrartcl, that would work, too....however, a solution to the actual problem being asked in the question would have worked, too.
    – DCTLib
    Commented Aug 15 at 10:47
  • 1
    @DCTLib The option halfparskip was already deprecated with TeX Live 2012 (the earliest I can check). With parskip=half, the redefinition I suggest works seamlessly from TL 2012 on.
    – egreg
    Commented Aug 15 at 12:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .