4

I'm working with a program that converts markup to LaTeX documents; customization of the output is limited to defining commands to insert in the preamble. A certain markup construct gets converted to LaTeX of the form:

\newcommand{\foo}{
Here Be Text
}

(Note that the newline before "Here Be Text" is inserted unconditionally by the converter, even though there is no corresponding newline in the markup.)

I would like to use \foo in the custom preamble to define a fancy header (\lhead{\textsc{\foo}}). The problem is that, due to the newline after \newcommand{\foo}{, the rendered document has a space inserted before "Here Be Text" in the header, so the header text is not flush with the hrule below it. The extra space goes away if a % is manually inserted at the end of \newcommand{\foo}{, but I would prefer not to have to do this.

My question is then: What can I insert in \lhead{\textsc{\foo}} to cause it to expand to \lhead{\textsc{Here Be Text}} instead of \lhead{\textsc{ Here Be Text}}?

A minimum working example of what I'm dealing with:

\documentclass{article}

\newcommand{\foo}{
Here Be Text
}

\usepackage{fancyhdr}
\pagestyle{fancy}
\lhead{\textsc{\foo}}
\chead{}
\rhead{\thepage}
\cfoot{}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua.  Ut enim ad minim veniam, quis
nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
fugiat nulla pariatur.  Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.
\end{document}

I have no idea what to attempt to fix this; my best idea was:

\lhead{\expandafter\textsc{%
\foo}}

but that didn't make a difference.

1
  • Why do you don't want to write {% in your definitions? Your decision will bring more problems if such definitions are used in various other contexts.
    – wipet
    Jun 29, 2020 at 6:53

3 Answers 3

7

Use

\lhead{\textsc{\ignorespaces\foo}}

instead

\lhead{\textsc{\foo}}

The \ignorespaces is TeX primitive working at min processor level, this is not problem in this case.

Edit: Another possibility is to temporarily disable making spaces at the end of lines and do your definitions in such environment:

\endlinechar=-1
   \newcommand \foo {
   Here Be Text
   }
\endlinechar=`\^^M
1
  • If you look in LaTeX itself, it uses \ignorespaces in many places where such an extra undesired space is prone to occur. Jun 29, 2020 at 7:00
3

There are a bunch of options. Here are two:

The fancy one

% Define \TrimSpacesApply
\ExplSyntaxOn
\cs_generate_variant:Nn \tl_trim_spaces_apply:nN { V }
\cs_new_eq:NN \TrimSpacesApply \tl_trim_spaces_apply:VN
\ExplSyntaxOff

% Use it
\lhead{\TrimSpacesApply\foo\textsc}

Here \TrimSpacesApply is a copy of \tl_trim_spaces_apply:VN. \tl_trim_spaces_apply:VN will trim the spaces of the first argument (\foo) and pass it as argument to the second (\scshape).

The dirty one:

\lhead{\expandafter\textsc\expandafter{\romannumeral-`f\foo}}

The \expandafters will expand \romannumeral, and \romannumeral will expand tokens looking for an integer followed by an optional space. \romannumeral will see -`f as the number -102 (you can use any letter you like in place of f), and will expand \foo and consume the leading space. The expansion of \romannumeral-102 will be nothing, because the number is negative.

The notation -`f is used, rather than just -102 in case \foo starts with a number: \romannumeral-1025 expands to nothing, whereas \romannumeral-`f5 expands to 5 (which is correct in your case).

2
\newcommand{\trim}[1]{\expandafter\xtrim#1}
\newcommand{\xtrim}[1]{#1}

lets you write

\lhead{\textsc{\trim\foo}}

Note: this is fragile if \foo can be empty.

You must log in to answer this question.

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