How to replace photo in moderncv classic with a QR code?
\photo
command wants a file suitable to be included with an \includegraphics
command. Therefore, I think the easiest solution would be to create your own qrcode within an standalone
document and use the result as photo in moderncv.
%File qrcode.tex -> qrcode.pdf
\documentclass{standalone}
\usepackage{qrcode}
\begin{document}
\qrcode[hyperlink,height=64pt]{http://www.ctan.org}
\end{document}
\documentclass[11pt,a4paper,sans]{moderncv}
\moderncvstyle{classic}
\usepackage{qrcode}
\usepackage{graphicx}
\firstname{John}
\familyname{Doe}
\address{83 Fancy Avenue}{Noweheresville}{Gotham City 24061}
\phone[mobile]{123 456 7890}
\email{[email protected]}
\photo[64pt][0.5pt]{qrcode}
\begin{document}
\makecvtitle
\end{document}
Update: All in one ;-)
With filecontents
package and an \immediate
command it's possible to join both files into one and compile both together:
\documentclass[11pt,a4paper,sans]{moderncv}
\moderncvstyle{classic}
\usepackage{qrcode}
\usepackage{filecontents}
\begin{filecontents*}{myqrcode.tex} %<- This is the qrcode file name
\documentclass{standalone}
\usepackage{qrcode}
\begin{document}
\qrcode[hyperlink,height=64pt]{http://www.ctan.org}
\end{document}
\end{filecontents*}
\immediate\write18{pdflatex myqrcode.tex}
\firstname{John}
\familyname{Doe}
\address{83 Fancy Avenue}{Noweheresville}{Gotham City 24061}
\phone[mobile]{123 456 7890}
\email{[email protected]}
\photo[64pt][0.5pt]{myqrcode} %<- Insert previous name
\begin{document}
\makecvtitle
\end{document}
In the code of class moderncv
, style classic
you can find an if-then-else construct checking if a \photo
is defined (printing it) or not (doing nothing). I added inside the empty case a new if-then-else testing, if a new command \qrphoto
is defined. If it is defined the code prints it, in the other case it does nothing.
The new command for the \qrphoto
is:
\NewDocumentCommand{\qrphoto}{O{64pt}O{0.4pt}m}{\def\@qrphotowidth{#1}\def\@qrphotoframewidth{#2}\def\@qrphoto{#3}}
With this command I define the needed values \@qrphotowidth
etc. for later usage.
Now we can patch the original command with
\patchcmd{\makecvhead}%
{%
\ifthenelse{\isundefined{\@photo}}{}%
}% code to patch
{% new code <=========================================================
\ifthenelse{\isundefined{\@photo}}%
{%
\ifthenelse{\isundefined{\@qrphoto}}%
{}%
{%
\if@left%
\hspace*{\separatorcolumnwidth}\fi%
\color{black}% <======================== to get a black qrcode
\setlength{\fboxrule}{\@qrphotoframewidth}%
\ifdim\@qrphotoframewidth=0pt%
\setlength{\fboxsep}{0pt}\fi%
\raisebox{1cm}{\framebox{\qrcode[hyperlink,height=\@qrphotowidth]{\@qrphoto}}}
}
}%
}% end new code <=====================================================
{}% success
{\fail}% failure
The command \raisebox[1cm]
is needed to get the qr-code on the right position (I did not search for the reason, why I need it here. The used value is okay for sizes of the resulting qr-code of 2cm (I used for the example code) or more. If your qr-code should be smaller you need to change my used value of 1cm
to a lower value. You will have to try it out.
As you can see I deleted the command \includegraphics
. With command \qrphoto
\qrcode... from package qrcode
.
With the command
\qrphoto[2cm][0.5pt]{https://tex.stackexchange.com/questions/474546/}
for example you can add an qr-code-image with a hight and width of 2cm
, a frame around it with a line of 0.5pt
and the content of the qr-code-image is https://tex.stackexchange.com/questions/474546/
(this question).
So with the following complete MWE
\documentclass[11pt,a4paper,sans]{moderncv}
\moderncvstyle{classic}
\usepackage{graphicx}
\usepackage{qrcode}
\firstname{John}
\familyname{Doe}
\address{83 Fancy Avenue}{Noweheresville}{Gotham City 24061}
\phone[mobile]{123 456 7890}
\email{[email protected]}
\makeatletter
\NewDocumentCommand{\qrphoto}{O{64pt}O{0.4pt}m}{\def\@qrphotowidth{#1}\def\@qrphotoframewidth{#2}\def\@qrphoto{#3}}
% to patch the code of moderncv, version 2.0.0
%\usepackage{etoolbox} % already loaded in moderncv <===================
\patchcmd{\makecvhead}%
{%
\ifthenelse{\isundefined{\@photo}}{}%
}% code to patch
{% new code <=========================================================
\ifthenelse{\isundefined{\@photo}}%
{%
\ifthenelse{\isundefined{\@qrphoto}}%
{}%
{%
\if@left%
\hspace*{\separatorcolumnwidth}\fi%
\color{black}% <======================== to get a black qrcode
\setlength{\fboxrule}{\@qrphotoframewidth}%
\ifdim\@qrphotoframewidth=0pt%
\setlength{\fboxsep}{0pt}\fi%
\raisebox{1cm}{\framebox{\qrcode[hyperlink,height=\@qrphotowidth]{\@qrphoto}}}
}
}%
}% end new code <=====================================================
{}% success
{\fail}% failure
\makeatother
%\photo[64pt][0.5pt]{example-image}
\qrphoto[2cm][0.5pt]{https://tex.stackexchange.com/questions/474546/} %
\begin{document}
\makecvtitle
\end{document}
you get the following result:
Please note that you can only print a photo or a qr-code. If you have a defined \photo
and \qrphoto
only the photo is printed!