1

I have a document (twopage) in which the left field is overlapping with the middle field. One solution is to put an \hspace{...} before the middle field but works for odd pages only. I'm trying to setup a tabularx. (tabular is crashing)

here is my MWE, the first part is with tabularx (its width does not do what specified) and the second part is with the \hspace

\documentclass[a4paper,twoside]{article}
\usepackage{lipsum,fancyhdr,tabularx,tabularray}

\pagestyle{fancy}
\fancyhf{}
\fancyhead[C]{\textbf{entête}}

\fancyfoot[CO]{%
  \begin{tabularx}{\textwidth}{@{}c|c|c@{}}
    here is the title of the chapter, sometime long  & book title & \thepage \tabularnewline
  \end{tabularx}
}

\fancyfoot[CE]{%
  \begin{tabularx}{\textwidth}{@{}c|c|c@{}}
    \thepage & book title & here is the title of the chapter, sometime long \tabularnewline
  \end{tabularx}
}
\begin{document}
  \lipsum{1}
  \newpage

\fancyfoot[C]{%
 \hspace{20em}book title 
}

\fancyfoot[LO,RE]{%
  here is the title of the chapter, sometimes long
}

\fancyfoot[RO,LE]{%
  \thepage
}

  \lipsum{2} 
\end{document}
4
  • 1
    Can you explain the output that you want? How, for example, should long chapter titles be handled? Should they wrap? And if so, how (bottom-aligned or top-aligned with the surrounding footer text)? Are you okay with supplying an alternative, shorter title?
    – Werner
    Commented Jun 30 at 16:43
  • 3
    \begin{tabularx}{\textwidth}{@{}c|c|c@{}} never use tabularx with no X column, it has no way to use the stated width. use tabular* here (if yo want a table at all, which seems unnecessary) fancyhdr already gives you the option to specify left centered and left parts, why use a tabular? Commented Jun 30 at 19:02
  • @Werner since i outlined the overlapping, my goal here is to get rid of the overlapping. Actually my solution of using \hspace{...} was the best solution. I did not think of putting it after the content to displace the middle field to the left. Commented Jul 1 at 11:35
  • word wrapping is actually one of the reason for which I thought of using a tabularx but at the time of writing this post i forgot to mention it. With this post i wanted to solve both the problem of tabularx width in the footer and the slight displacement towards right or left of the middle field in the footer. Today for the document I'm currently working on, the latter solution is fine. But in the future If I ever have very long strings I might have to use the tabularx option Commented Jul 1 at 11:53

2 Answers 2

3

Here are two options:

  1. Spread out the footer content evenly (using \hfills).

  2. Limit the possibly-long chapter title to 50% of the footer width, aligning it vertically to the baseline.

\documentclass[twoside]{article}

\usepackage{lipsum,fancyhdr}

\pagestyle{fancy}

\begin{document}

% Page style option 1: evenly spread left/center/right footer
\fancyhf{}% Clear header/footer
\fancyhead[C]{Centre header}

\fancyfoot[CO]{%
  A here is the title of the chapter, sometime long\hfill
  book title\hfill
  \thepage
}

\fancyfoot[CE]{%
  \thepage\hfill
  book title
  \hfill
  B here is the title of the chapter, sometime long
}

\lipsum[1-10]

\clearpage

% Page style option 2: Limit chapter title to 50% of footer width (bottom-aligned)
\fancyhf{}% Clear header/footer
\fancyhead[C]{Centre header}

\fancyfoot[CO]{%
  \parbox[b]{0.5\linewidth}{\raggedright A here is the title of the chapter, sometime long}\hfill
  book title\hfill
  \thepage
}

\fancyfoot[CE]{%
  \thepage\hfill
  book title
  \hfill
  \parbox[b]{0.5\linewidth}{\raggedleft B here is the title of the chapter, sometime long}
}

\lipsum[1-10]

\end{document}

Note that in both cases only the Centre footer element is used, which is placed in a box that spans the text width.

Here is the output of the first option:

enter image description here

A drawback of this option is that varying lengths of a chapter title (or page number width) will have the layout seem to change, as the gap is just filled with stretch (whatever is needed).

Here is the output of the second option:

enter image description here

A drawback of this option might be that there is mis-alignment (visually) with a too-short title - something that doesn't fill 50% of the footer width.

My recommendation is to provide an alternative, shorter title to avoid the overlap to begin with. Moreover, consider spreading the content to the header as well (further avoiding overlap opportunities).

3
  • the first option looks good and the 2nd too except that i would have preferred alignment at the top Commented Jul 16 at 14:27
  • 1
    @user1850133: Then you can adjust the vertical alignment/anchor (optional) argument of \parbox. It's currently set to the bottom; change it to the top.
    – Werner
    Commented Jul 16 at 16:10
  • i know, thanks... Commented Jul 17 at 9:53
2

The issue is simple: fancyhdr has done exactly what you asked:

\fancyfoot[C]{%
  \hspace{20em}book title 
}

20em is not a fixed width because it is font-dependent, but, in most cases, it is a significant chunk of the width of the text block. To see what is happening, just add x before the space,

\fancyfoot[C]{%
 x\hspace{20em}book title 
}

'x' marks the spot

Exactly the same happens on odd pages, but the result happens not to be overlapping text.

'x' marks the odd spot, too

So if you want to use long titles rather than shortened versions in the footers, you do need something other than the tripartite division provided by fancyhdr. To avoid that, you could tweak your hack to put the \hspace in the right place.

\fancyfoot[CO]{%
 \hspace{20em}book title%
}
\fancyfoot[CE]{%
  book title\hspace{20em}%
}

Alternatively, you could use a tabularx setup with or without fancyhdr's assistance. It's hard to think what alignment you have in mind here, but perhaps something like the following, which assumes the title is never so long as to run to a second line,

\fancyfoot[CO]{%
  \begin{tabularx}{\textwidth}{@{}l|>{\centering}X|r@{}}
    here is the title of the chapter, sometime long  & book title & \thepage \tabularnewline
  \end{tabularx}%
}

\fancyfoot[CE]{%
  \begin{tabularx}{\textwidth}{@{}l|>{\centering}X|r@{}}
    \thepage & book title & here is the title of the chapter, sometime long \tabularnewline
  \end{tabularx}%
}

As David explained, any tabularx must include at least one X column.

If the title might run to a second line, you need to allow for that and that means the chapter needs an/the X, which makes alignment trickier. In that case you really don't want vertical rules (which I wouldn't use anyway, but nobody would use them here), so maybe something like

\fancyfoot[CO]{%
  \begin{tabularx}{\textwidth}{@{}Xcr@{}}
    here is the title of the chapter, sometimes ridiculously, absurdly, incredibly, magnificently long  & book title & \hspace*{10mm}\thepage \tabularnewline
  \end{tabularx}%
}

\fancyfoot[CE]{%
  \begin{tabularx}{\textwidth}{@{}lc>{\raggedleft}X@{}}
    \thepage\hspace*{10mm} & book title & here is the title of the chapter, sometimes ridiculously, absurdly, incredibly, magnificently long \tabularnewline
  \end{tabularx}%
}

Or you could use tabular* rather than tabularx, as David also suggested.

I would post images, but I can't crop multipage output and conversion takes a significant amount of time and insists on converting all pages, so you'll have to compile and/or run them for yourself as Okular still haven't fixed the bug on X.

You must log in to answer this question.

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