[Tex/LaTex] Fitting fancyhdr’s chead in a colorbox

boxesfancyhdrheader-footer

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:

output

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}

\setlength{\headheight}{3cm}
\renewcommand{\headrulewidth}{0pt}

\lhead{\includegraphics[width=2cm]{Wikimedia-logo}}
\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}}}}}
\rhead{\includegraphics[width=2cm]{Wikimedia-logo}}

\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.

Best Answer

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}

\setlength{\headheight}{3cm}
\renewcommand{\headrulewidth}{0pt}

\lhead{\raisebox{-0.3\height}{\makebox[0pt]{\includegraphics[width=2cm]{Wikimedia-logo}}}}%
\chead{ \colorbox{ctagblue}{\parbox[b][][c]{10cm}{\textcolor{white}{\sffamily\bfseries loasdf sdofkjaosdjgas jkgaslk gjlsjasdlgk jasg alksdfj sdklfj alkdfj laskdfjasdf}}}}
\rhead{\raisebox{-0.3\height}{\makebox[0pt]{\includegraphics[width=2cm]{Wikimedia-logo}}}}

\begin{document}

\chapter{Test}
\lipsum

\end{document} 

enter image description here

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}}}}%
\chead{\setlength\fboxsep{0pt} \colorbox{ctagblue}{\parbox[b][1.8cm][c]{10cm}{\centering\color{white}\sffamily\bfseries loasdf sdofkjaosdjgas jkgaslk gjlsjasdlgk jasg alksdfj sdklfj alkdfj laskdfjasdf}}}
\rhead{\raisebox{-1mm}{\makebox[0pt]{\includegraphics[height=2cm]{Wikimedia-logo}}}}

enter image description here

Related Question