3

In this MWE, if I replace the fancyfoot line with the next one (with color{black}), the page number goes lower (and is even cut).

Why is there a difference between the two versions? I would expect them to do produce the same thing: a black page number at the exact same place.

By the way, I put a "\setlength{\footskip}{50pt}" because I had a warning with the first version (the color-free one). Maybe it has something to do with the explanation...?

\documentclass{article}
\usepackage[a4paper,top=2cm, bottom=2.5cm, left=2cm, right=2cm, twoside=true]{geometry}
\usepackage{fancyhdr}
\usepackage{fontspec}
\usepackage{xcolor}
\begin{document}
\fancypagestyle{plain}{%
\fancyhf{}%
}%
\pagestyle{plain}%
\setlength{\footskip}{50pt}
\fancyfoot[RO, LE]{\fontspec{Arial}\fontsize{50pt}{60pt}\selectfont\thepage}%
%\fancyfoot[RO, LE]{\fontspec{Arial}{\color{black}{\fontsize{50pt}{60pt}\selectfont\thepage}}}%
some text
\end{document}

EDIT: I'm using the lualatex command (lualatex somefilename.tex) on an Ubuntu 22.04 OS (compilation log: "This is LuaHBTeX, Version 1.14.0 (TeX Live 2022/dev/Debian)"). Here are the results:

  • without nor color nor textcolor, or with textcolor: same result (page number not cut) withoutColor
  • with color: page number cut withColor
Michaël
  • 124
  • 1
    Replace \color with \textcolor? – egreg Jun 04 '22 at 10:03
  • I am unable to reproduce the effect you say you're getting -- indeed, the page number is placed in the exact same location. Which TeX distribution do you employ? Do you use XeLaTeX or LuaLaTeX? (FWIW, my TeX distribution is MacTeX2022, all updates applied; I get the same (non-)result when compiling your code with XeLaTeX or LuaLaTeX.) – Mico Jun 04 '22 at 10:05
  • @Mico I'm using LuaLaTeX, I edited my question. And @egreg indeed, replacing \color by \textcolor gives the same result (= the page number at the same position) as without \color. Why the difference?! – Michaël Jun 04 '22 at 11:32
  • 1
    This has been solved in fancyhdr version 4.0.2. But using \textcolor is the best, I think. – Pieter van Oostrum Jun 04 '22 at 12:44
  • You can also fix the problem my moving footskip=50pt into geometry BEFORE loading fancyhdr. Fancyhdr does not respond well to changes in page geometry. – John Kormylo Jun 04 '22 at 14:02

2 Answers2

3

I can't reproduce the behavior, but

  1. using \fontspec is a waste of resources;
  2. the definition of the page style is uselessly confusing;
  3. the setting of the footskip should be done with geometry;
  4. \color{black}{...} should be \textcolor{black}{...}.
\documentclass{article}
\usepackage[
  a4paper,
  top=2cm,
  bottom=2.5cm,
  left=2cm,
  right=2cm,
  twoside=true,
  footskip=50pt,%<--- change to suit
]{geometry}
\usepackage{fancyhdr}
\usepackage{fontspec}
\usepackage{xcolor}

\usepackage{lipsum}

\newfontfamily{\arial}{Arial}

\fancypagestyle{plain}{% \fancyhf{}% \fancyfoot[RO, LE]{\textcolor{black}{\fontsize{50pt}{60pt}\arial\thepage}}% \renewcommand{\headrulewidth}{0pt}% } \pagestyle{plain}

\begin{document}

\lipsum[1-20]

\end{document}

enter image description here

egreg
  • 1,121,712
  • Thanks for all the cleaning and the good advice! \textcolor indeed gives me what I want. But why would the resuls of \textcolor and \color differ on my computer (and not yours)? Could something be broken in my TeX installation? And what is the difference in nature between \textcolor and \color? (even if \textcolor's purpose is probably obvious, given its name...) – Michaël Jun 04 '22 at 11:52
  • @Michaël Because \color{black} is a declaration: “from now on, print in black”. It doesn't take arguments. Conversely, \textcolor{black}{text} is a command: “print ‘text’ in black, otherwise keep the current color”. – egreg Jun 04 '22 at 11:54
  • @Michaël It could be difference in TeX live distribution version, the compiler used etc.. For the difference there's already What is the difference between \textcolor and \color? - TeX - LaTeX Stack Exchange – user202729 Jun 04 '22 at 11:57
3

IMHO, the different spacing with \color comes from the fact that, in the provided example, the \color macro is processed while TeX is in (internal) vertical mode. Indeed, if you insert \ifvmode\ifinner\typeout{We are in internal vertical mode}\fi\fi right before \color in the MWE, TeX will print “We are in internal vertical mode” (tested with LuaTeX).

It is a well-known fact that whatsits occurring in vertical mode may affect vertical spacing:

  • because they prevent a subsequent \addvspace from “seeing” beyond the whatsit (see David Carlisle's example here or imagine an \addvspace followed by a whatsit itself followed by a second \addvspace);

  • because a whatsit at the start of a \vtop may modify the \vtop's reference point (according to TeX's rules).

These two possible reasons are given in the footnote on p. 6 of grfguide.pdf; there may be others.

On the contrary, when xcolor is loaded, \textcolor uses \leavevmode to switch to horizontal mode before appending the color whatsit to the current list:

\def\textcolor#1#{\@textcolor{#1}}
\def\@textcolor#1#2#3{\protect\leavevmode{\color#1{#2}#3}}

Therefore, when \textcolor is used (as opposed to \color), you are sure that its second mandatory argument will be processed in horizontal mode, and as a consequence shouldn't affect vertical spacing.

As David Carlisle wrote in the above link:

The trick is to do colour changes as far as possible in horizontal not vertical mode

frougon
  • 24,283
  • 1
  • 32
  • 55