# Fitting fancyhdr's chead in a colorbox

I am trying to create a header using fancyhdr in which the rhead and lhead parts are two images, of the same and known size.

In the center, I would like to put some text (perhaps the document's title), but when it is too long, it does not fit in the header:

How could I force that line to break and fit in the colorbox? Should I use any other approach instead?

Here is what I have tried so far:

\documentclass[a4paper,10pt,onecolumn]{article}

% Packages needed
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}         % Latin Modern fonts
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{lipsum}
\usepackage{xcolor}

\graphicspath{{./images/}}

\definecolor{ctagblue}{HTML}{003366}

\usepackage{fancyhdr}
\pagestyle{fancy}

\chead{\parbox[b][21mm][c]{10cm}{\colorbox{ctagblue}{\makebox[10cm][c]{\vrule height 1cm depth 1cm width 0pt \textcolor{white}{\bfseries loasdf sdofkjaosdjgas jkgaslk gjlsjasdlgk jasg alksdfj sdklfj alkdfj laskdfjasdf}}}}}

\begin{document}
\chapter{Test}
\lipsum
\end{document}


The logo image has been taken from wikimedia.org.

## EDIT 1

I would like to keep the box with a fixed dimension as shown in the image, so the height should be constant even when the text has one or multiple lines.

## EDIT 2

Would it be possible to fit the text in the box even when it is very long? That would mean to automatically scale the font.

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – cfr Sep 17 '14 at 23:32

Another solution would be to use the package tcolorbox, which will allow any text to properly fit in the box (no matters its length):

% [...]
\usepackage[many]{tcolorbox}
% [...]
\chead{\parbox[b][21mm][c]{10cm}{\tcboxfit[colback=ctagblue,colframe=black,width=10cm,height=2cm,fit basedim=20pt,fit fontsize macros,valign=center,frame style={fill=ctagblue}]{\centering \textcolor{white}{\bfseries Test Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.}}}}
% [...]


If it is possible, the basedim text size will be used for the text to fit in the box:

Otherwise, when the text is too long to fit in the box with basedim size, it is automatically resized (reduced):

This may be the best solution for a document template, as you never know which text will be placed there in the future, nor its length.

See complete code here:

\documentclass[a4paper,10pt,onecolumn]{article}

% Packages needed
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}         % Latin Modern fonts
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[many]{tcolorbox}

\graphicspath{{./images/}}

\definecolor{ctagblue}{HTML}{003366}

\usepackage{fancyhdr}
\pagestyle{fancy}

\chead{\parbox[b][21mm][c]{10cm}{\tcboxfit[colback=ctagblue,colframe=black,width=10cm,height=2cm,fit basedim=20pt,fit fontsize macros,valign=center,frame style={fill=ctagblue}]{\centering \textcolor{white}{\bfseries Test Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae, felis.}}}}

\begin{document}
\chapter{Test}
\lipsum
\end{document}


You could set the entire heading using \chead and arrange things inside a tabular for better management of widths:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{array}

\definecolor{ctagblue}{HTML}{003366}

\usepackage{fancyhdr}
\pagestyle{fancy}

\begin{tabular}{m{2cm}p{5pt}m{\dimexpr\textwidth-4cm-10pt}p{5pt}m{2cm}}
\includegraphics[width=2cm,height=2\baselineskip,valign=c]{example-image-a} &
\cellcolor{ctagblue} &
\cellcolor{ctagblue}\textcolor{white}{\bfseries loasdf sdofkjaosdjgas jkgaslk gjlsjasdlgk jasg alksdfj sdklfj alkdfj laskdfjasdf} &
\cellcolor{ctagblue}&
\includegraphics[width=2cm,height=2\baselineskip,valign=c]{example-image-b}
\end{tabular}}

\begin{document}
Some text.
\end{document}


Some considerations regarding the above code:

• adjustbox (with the [export] option) loads graphicx and allows for centering content vertically (via valign=c).

• array provides an m{<len>} column for vertical anchoring of cells.

• Two additional (empty) columns were created to create a \tabcolsep-like gap between the images and the paragraph text.

I think you just need fewer boxes ;).

\documentclass[a4paper]{article}

% Packages needed
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}         % Latin Modern fonts
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{lipsum}
\usepackage{xcolor}

\definecolor{ctagblue}{HTML}{003366}

\usepackage{fancyhdr}
\pagestyle{fancy}

\parbox[b][21mm][c]{10cm}{%
\centering
\textcolor{white}{\bfseries loasdf sdofkjaosdjgas jkgaslk gjlsjasdlgk jasg alksdfj sdklfj alkdfj laskdfjasdf}%
}%
}%
}

\begin{document}
\chapter{Test}
\lipsum
\end{document}


Note that I've used the tiger image because it is part of a standard TeX distribution. Also, 10pt and onecolumn are default for article.

To 'flip' the tiger in accordance with Bernard's suggestion, you can use \reflectbox{}. For example:

\rhead{\reflectbox{\includegraphics[width=2cm]{tiger}}}


if you would like to protect the header from harm:

or

\lhead{\reflectbox{\includegraphics[width=2cm]{tiger}}}


if your text is otherwise liable to escape:

• May I suggest to flip the left tiger? – Bernard Sep 18 '14 at 0:11
• @Bernard That's a good idea (which occurred to me, too) but I was only using the tiger as a place-holder because I was too lazy to download the wikimedia logo! – cfr Sep 18 '14 at 0:20
• +1. Great solution and thanks for the comments and suggestions too! I will wait before accepting the answer and see if my answer may also be interesting for others, as it allows for automatically resizing of the text inside the box. Comments/improvements on my solution are very welcome. :-) – Peque Sep 18 '14 at 0:21
• @cfr: It was only a suggestion so the text in the header can't escape. Or add bars to the \parjail. – Bernard Sep 18 '14 at 0:24
• @Bernard See my edit. I initially assumed you meant flip the right tiger as that's what had occurred to me. Then I reread your comment so I flipped the left one instead... – cfr Sep 18 '14 at 0:31

Here is a solution, using the natural height of the \parbox, no \makeboxused in \chead (and simpler code!). I also suggest the text in the header be in sans serif, to match the style of the logo (this may depeng on the real logo, of course). And the logo is rather large for a header, I suggest to let it partially flow into the margin:

\documentclass[a4paper,10pt]{article}

% Packages needed
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern} % Latin Modern fonts
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{geometry}
\usepackage{lipsum}
\usepackage{xcolor}

\graphicspath{{./images/}}

\definecolor{ctagblue}{HTML}{003366}

\usepackage{fancyhdr}
\pagestyle{fancy}

\begin{document}

\chapter{Test}
\lipsum

\end{document}


If you want the same height for the logo and the centre part of the header, replace the code for the header with this one (for a real height of 2cm, I need a parbox of 1.8 cm — I don't know why):

\lhead{\raisebox{-1mm}{\makebox[0pt]{\includegraphics[height=2cm]{Wikimedia-logo}}}}%