Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I want to place a thick vertical line next to my page number in the header of every page. I have chosen to build a small table and add the line to it. It works fine, until I add the option (part of the {tabu} package) of defining the thickness of the table border. The code (that generates many errors) looks like this:

\documentclass{book}

\usepackage{fancyhdr}
\usepackage{tabu}
\begin{document}
\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}%no top ruler
\fancyheadoffset{0.8 cm}
\fancyhead[RO]{\begin{tabu}{|[2pt]c}\bfseries\thepage \\ ~ \end{tabu}}
\fancyhead[LE]{\begin{tabu}{c|[2pt]}\bfseries\thepage \\ ~ \end{tabu}}
\headheight=24pt
~
\newpage
~
\end{document}

when I put the table in the main text (i.e. not in the header) they come out fine

\begin{tabu}{|[2pt]c}\bfseries\thepage \\ ~ \end{tabu}

when I remove the [2pt] line there is no problem either. Nevertheless, I can't make out from the generated errors what went wrong. For instance, the first error reads:

!missing number, treated as zero

When I compile this as part of my larger project, the compilation process crashes entirely!

My questions would be: Why is this code not working inside of headers and how should it be changed to obtain the desired effect?

share|improve this question
add comment

2 Answers

enter image description here

Unfortunately the tabu package is often incompatible with latex (most notably in its unfortunate decisions to change the > syntax, here it is tripping over active characters.

\documentclass{book}

\usepackage{fancyhdr}
\usepackage{tabu}



\pagestyle{fancy}
\fancyhead{}
\fancyfoot{}
\renewcommand{\headrulewidth}{0pt}%no top ruler
\fancyheadoffset{0.8 cm}
\makeatletter
\fancyhead[RO]{\let\@activechar@info\@gobble\begin{tabu}{|[2pt]c}\bfseries\thepage \tabularnewline ~ \end{tabu}}
\fancyhead[LE]{\let\@activechar@info\@gobble\begin{tabu}{c|[2pt]}\bfseries\thepage \tabularnewline ~ \end{tabu}}
\makeatother
\headheight=24pt
\begin{document}
~ 
\newpage
~
\end{document}
share|improve this answer
    
Many thanks, this indeed works splendidly! Do I understand correctly that the code you have added finds and suppresses the use of any active characters by the {tabu} package? –  skleijn Apr 10 '13 at 11:37
    
@skleijn the description you give makes it sound like a rather grander project than it was:-): better to say it is a workaround that locally disables some code in the standard output routine to avoid the particular error that your example showed. all \@activechar@info does is make a warning that active characters are being used in an unsafe place, but the warning itself messes up tabu's processing of the array preamble so I silence the warning. –  David Carlisle Apr 10 '13 at 11:47
add comment

You're using a sledgehammer. Better measurement can be obtained by using rules and doing carefully the alignment.

Here's a possibility (the showframe option and the geometry package are used just for the example):

\documentclass{book}

%% the following two packages are just for the example
\usepackage[pass,showframe]{geometry}
\usepackage{lipsum}
%%

\usepackage{calc}
\usepackage{fancyhdr}

\pagestyle{fancy}
\renewcommand{\headrulewidth}{0pt}%no top ruler
\fancyhf{}
\fancyhead[RO]{\rightpage}
\fancyhead[LE]{\leftpage}

\setlength{\headheight}{24pt}

\newcommand{\digitheight}{\fontcharht\font`0 }
\newcommand{\rightpage}{\mypagenumber{\ }{l}{\ \ \bfseries\thepage}}
\newcommand{\leftpage}{\mypagenumber{\bfseries\thepage\ \ }{r}{\ }}
% Arguments of \mypagenumber:
% #1: tokens to be set at the left of the bar
% #2: position of the bar (use l for the right header, r for the left)
% #3: tokens to be set at the right of the bar
\newcommand{\mypagenumber}[3]{%
  \raisebox{\ht\strutbox-\digitheight}{%
    \makebox[0pt][#2]{%
      \bfseries
      #1%
      \vrule depth \dimexpr 24pt - \digitheight\relax width 2pt
      #3}%
  }%
}

\begin{document}

\lipsum[1-12]
\end{document}

Page 1

enter image description here

Page 2

enter image description here

Page 1 without frames

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.