5

I am making my resume using the moderncv package with the classic style.

I wish to replace the photo with a QR-code to my personal website, using the qrcode package.

I tried replacing the argument of the \photo command with the corresponding \qrcode{...} command, but this fails with a LaTeX error message:

\photo[64pt][0.5pt]{\qrcode[hyperlink,height=64pt]{http://www.ctan.org}}

Here is a MWE that illustrates the idea I am trying:

\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{someone@xyz.com}

% \photo[64pt][0.5pt]{example-image-a}  % works fine
\photo[64pt][0.5pt]{\qrcode[hyperlink,height=64pt]{http://www.ctan.org}} % produces an error

\begin{document}
   \makecvtitle
\end{document}

How can I achieve the desired goal?

Update

I now understand that \photo is essentialy a wrapper for includegraphics, and therefore expects a file path and/or file name as its argument.

  • Is it possible to re-define \photo to be flexible enough to accept either a latex command or a graphics file path and/or file name?

  • Failing that, at least is it possible to automate the inclusion of a QR-code image that automatically updates whenever the URL argument to the \qrcode command is updated i.e. avoid compilation of multiple documents?

  • Are you loading the qrcode package? I don't think moderncv loads it, or at least it didn't last time I checked, which was a few years ago – Chris H Feb 12 at 16:51
  • @ChrisH yes, my MWE forgot to include it. Updated the mwe suitably. The errror is not due to this. – Krishna Feb 12 at 16:52
  • It seems that the argument of \photo is a graphics file, not a LaTeX command. Have you tried to compile the qrcode to a pdf file, and to include the latter? – marmot Feb 12 at 16:59
  • @marmot that doesn't sound like an elegant option to me. Is it possible to redefine \photo to accept both LaTeX commands as well as as image file paths? – Krishna Feb 12 at 17:01
  • @marmot If I want to update the qr code in the future, I have to remember to re-compile the qr code image. That's tedious and error-prone. At the very least can we have one single compilation that automatically updates the image whenever the qrcode command arguments are updated? – Krishna Feb 12 at 17:03
3

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{someone@xyz.com}

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

resulting pdf

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!

  • this produces a coloured QR code when the CV colour is set eg. \moderncvcolor{blue} produces a QR code with blue dots instead of black – Krishna Feb 13 at 17:49
  • I will accept the answer for now since it works brilliantly. Whenever you have a chance, please edit the answer by changing the argument of \color from color1 to black – Krishna Feb 13 at 17:51
  • You can simply change the used code \color{color1} (color1 is then set to blue, if that is your used main color for the cv) to \color{black}. – Kurt Feb 13 at 17:55
  • It is officially recommended that there must be an extra margin/white-space padding around any QR code. I wish to get rid of the fbox around it by setting \qrphoto[2cm][0pt], but in this case, the base of the QR code does not align with the base of the email address, but is rather lifted upwards. How to solve this issue? – Krishna Feb 13 at 17:58
  • @Krishna change \raisebox{1cm} to \raisebox{0.9cm} in that case. – Kurt Feb 13 at 18:03
8

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

enter image description here

\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{someone@xyz.com}

\photo[64pt][0.5pt]{qrcode}

\begin{document}
   \makecvtitle
\end{document}

enter image description here

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{someone@xyz.com}

\photo[64pt][0.5pt]{myqrcode} %<- Insert previous name

\begin{document}
   \makecvtitle
\end{document}
  • @lgnasi thank you. But this does not look like a nice option.Is it possible to redefine \photo to accept both LaTeX commands as well as as image file paths? If I want to update the qr code in the future, I have to remember to compile the original image. That's tedious and error-prone. At the very least can we have one single compilation that automatically updates the image whenever the qrcode command arguments are updated? – Krishna Feb 12 at 17:03
  • @Krishna You can do it with filecontents help. See my updated answer. – Ignasi Feb 12 at 17:31
  • @lgnasi Yes, just finished trying your combined solution with the filecontents package. It works! Thank you. Upvoted and marked as accepted. – Krishna Feb 12 at 18:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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