# Trying to add an image/icon just before the text in a section while using moderncv

Note: I'm using sharelatex

I was wondering if there are any way to set the background color of the header part (the one that contains personal info) of a file that uses moderncv in order to visually differentiate the header and the content.

I tried with minipage, \hfill and so on but I guess that moderncv uses a heavily customized template and everything I've tried so far lead to a compile error.

\section{\includegraphics{../static/32x32/image.png}Expériences Professionnelles}


The error:

Error in fr/source.tex (line 54): Use of \@newline doesn't match its definition.
\@ifnextchar ... \reserved@d =#1\def \reserved@a {
#2}\def \reserved@b {#3}\f...
l.54 ...image.png}Expériences Professionnelles}

If you say, e.g., \def\a1{...}', then you must always
put 1' after \a', since control sequence names are
made up of letters only. The macro here has not been
followed by the required stuff, so I'm ignoring it.

• The title of thq question talks about an image or icon before a section, but the body of the question talks about a background color (which was asked before and has been answered in tex.stackexchange.com/q/163905/3954). Mar 6 '14 at 14:58
• Where should the image appear? Before the colored bar, between the bar and the title? Mar 6 '14 at 15:01

Here's one option. I redefined \section as implemented for the classic style so now you have a third optional argument allowing you to place anything (in particular, an image) before the section:

\documentclass[11pt,a4paper,sans]{moderncv}
\moderncvstyle{classic}
\moderncvcolor{blue}
\usepackage[scale=0.75]{geometry}

\firstname{John}
\familyname{Smith}

\title{Curriculum Vitae}
\mobile{(000) 000 0000}
\email{uname@email.com}

\makeatletter
\RenewDocumentCommand{\section}{smo}{%
\phantomsection{}% reset the anchor for hyperrefs
\IfNoValueTF{#3}
{}
{\leavevmode\llap{#3\hspace{10pt}}}%
\parbox[t]{\hintscolumnwidth}{\strut\raggedleft\raisebox{\baseletterheight}{\color{color1}\rule{\hintscolumnwidth}{0.95ex}}}%
\hspace{\separatorcolumnwidth}%
\parbox[t]{\maincolumnwidth}{\strut\sectionstyle{#2}}
}
\makeatother

\begin{document}

\makecvtitle

\section{Coursework}[\includegraphics[height=20pt]{example-image-a}]
\section{Coursework}[\includegraphics[height=20pt]{example-image-b}]
\section{Coursework}

\end{document}


If the image must appear between the colored bar and the title, the redefinition is a little bit different:

\documentclass[11pt,a4paper,sans]{moderncv}
\moderncvstyle{classic}
\moderncvcolor{blue}
\usepackage[scale=0.75]{geometry}

\firstname{John}
\familyname{Smith}

\title{Curriculum Vitae}
\mobile{(000) 000 0000}
\email{uname@email.com}

\makeatletter
\RenewDocumentCommand{\section}{smo}{%
\phantomsection{}% reset the anchor for hyperrefs
\parbox[t]{\hintscolumnwidth}{\strut\raggedleft\raisebox{\baseletterheight}  {\color{color1}\rule{\hintscolumnwidth}{0.95ex}}}%
\hspace{\separatorcolumnwidth}%
\IfNoValueTF{#3}
{\parbox[t]{\maincolumnwidth}{\strut\sectionstyle{#2}}}
{\parbox[t]{\maincolumnwidth}{\strut#3\ \sectionstyle{#2}}}%
}
\makeatother

\begin{document}

\makecvtitle

\section{Coursework}[\includegraphics[height=20pt]{example-image-a}]
\section{Coursework}[\includegraphics[height=20pt]{example-image-b}]
\section{Coursework}

\end{document}


• Awesome, works like a charm ! One question though: why do you need to use \phantomsection{} ? Mar 6 '14 at 15:43
• @Ketouem \phantomsection` is from the original definition; it's required to create an anchor for cross-references. Mar 6 '14 at 15:47