2

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.
2
  • 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, 2014 at 14:58
  • Where should the image appear? Before the colored bar, between the bar and the title? Mar 6, 2014 at 15:01

1 Answer 1

2

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}
\address{Street}{City, St. 1111}
\mobile{(000) 000 0000}
\email{[email protected]}

\makeatletter
\RenewDocumentCommand{\section}{smo}{%
  \par\addvspace{2.5ex}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{section}{#2}%
  \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}}
  \par\nobreak\addvspace{1ex}\@afterheading%
}
\makeatother

\begin{document}

\makecvtitle

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

\end{document}

enter image description here

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}
\address{Street}{City, St. 1111}
\mobile{(000) 000 0000}
\email{[email protected]}

\makeatletter
\RenewDocumentCommand{\section}{smo}{%
  \par\addvspace{2.5ex}%
  \phantomsection{}% reset the anchor for hyperrefs
  \addcontentsline{toc}{section}{#2}%
  \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}}}%
  \par\nobreak\addvspace{1ex}\@afterheading%
}
\makeatother

\begin{document}

\makecvtitle

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

\end{document}

enter image description here

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .