3

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.

  • 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
2

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:

enter image description here

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

enter image description here

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}

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

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

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

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

enter image description here

\documentclass{article}

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

\definecolor{ctagblue}{HTML}{003366}

\usepackage{fancyhdr}
\pagestyle{fancy}

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

\chead{\setlength{\tabcolsep}{0pt}%
  \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.

1

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}

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

\lhead{\includegraphics[width=2cm]{tiger}}
\chead{\colorbox{ctagblue}{%
    \parbox[b][21mm][c]{10cm}{%
      \centering
      \textcolor{white}{\bfseries loasdf sdofkjaosdjgas jkgaslk gjlsjasdlgk jasg alksdfj sdklfj alkdfj laskdfjasdf}%
    }%
  }%
}
\rhead{\includegraphics[width=2cm]{tiger}}

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

tiger header

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:

tigers on watch

or

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

if your text is otherwise liable to escape:

tigers on guard

  • 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
  • 1
    @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
1

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

  • Thanks for your answer! I would like the box to have the same height as the images, though. (I will update the question). – Peque Sep 18 '14 at 0:25
  • @Peque: OK, I'll add that. – Bernard Sep 18 '14 at 0:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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