7

I am trying to duplicate the shields used in Zenodo, Github, and so on to show a DOI, a project's status, and other snippets of information. These badges are actually served by shields.io, and look like this:

enter image description here

Unfortunately, as far as I know, one cannot call shields.io from LaTeX and so it's necessary to hack something up.

My approach has been to wedge together two \tcolorboxes.

So far I have coded this:

\documentclass{standalone}
\usepackage[utf8]{inputenc}

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0}}

\RequirePackage[usenames,tables]{xcolor}
\definecolor{ABlue}{HTML}{127bca}

% --------------------
% Text blocks
% --------------------
\usepackage{tcolorbox}

%define badges
\newcommand{\NEbox}[3]{%
    \begingroup
    \setlength{\fboxsep}{4pt}%  
    \fcolorbox{#1}{#2}{#3}%
    \endgroup
}
\definecolor{LHScolor}{HTML}{555555}

\newcommand{\DOIbox}[1]{%
    \NEbox{LHScolor}{LHScolor}{\textcolor{white}{DOI}}%
    \NEbox{ABlue}{ABlue}{\href{http://dx.doi.org/#1}{\textcolor{white}{#1}}}%
}

\begin{document}

\DOIbox{1234/5678}

\end{document}

The result looks like this at the moment:

enter image description here

Its close, but not quite there. I know how to modify the text and box colours, but the box vertical sizes are off. How do I change the height of the boxes so that they are the same?

(For added karma, is there a better way to create the badges that doesn't involve Tikz, which just scares me?)

EDIT: The shields.io implementation is a clickable graphic that links to the DOI parser. I have included this in the MWE and need a solution that implements this too.

  • add a \strut to before #3 of \fcolorbox so they are the same height. – David Carlisle Sep 16 at 20:02
  • @DavidCarlisle - thank you! Feel free to submit this as an answer and I'll accept it. – Andy Clifton Sep 16 at 20:15
  • I just posted an update of my previous answer, it seems to get closer to your request – BambOo Sep 17 at 11:36
  • 1
    You could just call system commands lke wget or curl from LaTeX to download the PNGs from shields.io during compilation. However, you have to run it as latex --shell-escape for this to work. – Fritz Sep 17 at 11:56
  • 1
    @fritz - nice idea! Unfortunately I can't use solutions that need system commands, as they don't work on overleaf or other web-based implementations (which I use a lot). Thanks though! – Andy Clifton Sep 17 at 12:28
7

Another idea (not yet packaged in a command) with a sidebyside tcolorbox

\documentclass[border=1mm]{standalone}
\usepackage[utf8]{inputenc}

\usepackage{hyperref}
\hypersetup{pdfborder={0 0 0}}

\RequirePackage[usenames,tables]{xcolor}
\definecolor{ABlue}{HTML}{127bca}
\definecolor{LHScolor}{HTML}{555555}
% --------------------
% Text blocks
% --------------------
\usepackage[most]{tcolorbox}
\tcbset
    {
        enhanced,top=0pt,left=0pt,right=0pt,bottom=0pt,
        enhanced,hbox,bicolor,sidebyside,
        colback=LHScolor,colbacklower=ABlue,
        fontupper=\color{white},fontlower=\color{white},
        lefthand width=1em,
        width=10em,
        size=small,boxrule=0pt,rounded corners
    }

\begin{document}


\begin{tcolorbox}[]
DOI
\tcblower
1234/5678
\end{tcolorbox}

\end{document}

enter image description here

EDIT : An improved version with automatic computation of box dimensions, color shading and \newcommand definition

\documentclass[border=1mm]{standalone}
\usepackage[utf8]{inputenc}
\RequirePackage[usenames,tables]{xcolor}
\definecolor{ABlue}{HTML}{127bca}
\definecolor{LHScolor}{HTML}{555555}
% --------------------
% Text blocks
% --------------------
\usepackage[most]{tcolorbox}
\newcommand{\DOIbox}[1]{
\tcbsidebyside[
        bicolor,
        sidebyside,
        %fontupper=\color{white},fontlower=\color{white},%  <-- broken by sidebyside adapt (explicitely set in input)
        sidebyside adapt=both,
        sidebyside gap=5pt,
        top=0pt,left=0pt,right=0pt,bottom=0pt,
        boxrule=0pt,rounded corners,
        interior style={top color=LHScolor,bottom color=LHScolor!60!black},
        segmentation style={top color=ABlue,bottom color=ABlue!60!black},
]{%
\color{white}DOI%
}{%
\color{white}#1%
}%
}

\begin{document}

\DOIbox{1}
\DOIbox{12}
\DOIbox{123}
\DOIbox{1234}
\DOIbox{12345}

\end{document}

enter image description here

EDIT n°2 (hopefully final one) Addition of a drop shadow (thanks to @Eric Marsden for the idea and motivation).

\documentclass[border=1mm]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[scaled]{helvet}
\renewcommand*{\familydefault}{\sfdefault}
\RequirePackage[usenames,tables]{xcolor}
\definecolor{ABlue}{HTML}{127bca}
\definecolor{LHScolor}{HTML}{555555}
\usepackage{graphicx}

% --------------------
% Text blocks
% --------------------
\usepackage[most]{tcolorbox}

% Creation of a dropped shadow
\newcommand{\droptextshadow}[2]{%
    \tikz[baseline,outer sep=0pt, inner sep=0pt]{
    % Dropped Shadow
    \node[#1!40!black] at (0,-0.2ex) {#2};
    % Text
    \node[white] at (0,0) {#2};
}%
}
\newcommand{\DOIbox}[1]{
\tcbsidebyside[
        bicolor,
        sidebyside,
        %fontupper=\color{white},fontlower=\color{white},%  <-- broken by sidebyside adapt (explicitely set in input)
        sidebyside adapt=both,
        sidebyside gap=5pt,
        top=0pt,left=0pt,right=0pt,bottom=0pt,
        boxrule=0pt,rounded corners,
        interior style={top color=LHScolor,bottom color=LHScolor!60!black},
        segmentation style={top color=ABlue,bottom color=ABlue!60!black},
]{%
\droptextshadow{LHScolor}{DOI}% <-- Drop shadow + text for DOI 
}{%
\droptextshadow{ABlue}{#1}% <-- Drop shadow + text for reference number
}%
}

\begin{document}
\DOIbox{10.5281/zenodo.241372}
\end{document}

enter image description here

EDIT n°3 Addition of hyperref with option hidelinks to make the box clickable and slight shadow settings modification to reduce shadow footprint

\documentclass[border=1mm]{standalone}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[scaled]{helvet}
\renewcommand*{\familydefault}{\sfdefault}
\RequirePackage[usenames,tables]{xcolor}
\definecolor{ABlue}{HTML}{127bca}
\definecolor{LHScolor}{HTML}{555555}
\usepackage[hidelinks]{hyperref}

% --------------------
% Text blocks
% --------------------
\usepackage[most]{tcolorbox}

% Creation of a dropped shadow
\newcommand{\droptextshadow}[2]{%
    \tikz[baseline,outer sep=0pt, inner sep=0pt]{
    % Dropped Shadow
    \node[#1!40!black] at (0,-0.1ex) {#2};
    % Text
    \node[white] at (0,0) {#2};
}%
}
\newcommand{\DOIbox}[1]{
\tcbsidebyside[
        bicolor,
        sidebyside,
        %fontupper=\color{white},fontlower=\color{white},%  <-- broken by sidebyside adapt (explicitely set in input)
        sidebyside adapt=both,
        sidebyside gap=5pt,
        top=0pt,left=0pt,right=0pt,bottom=0pt,
        boxrule=0pt,rounded corners,
        interior style={top color=LHScolor,bottom color=LHScolor!60!black},
        segmentation style={top color=ABlue,bottom color=ABlue!60!black},
]{%
\droptextshadow{LHScolor}{DOI}% <-- Drop shadow + text for DOI 
}{%
\droptextshadow{ABlue}{\href{http://dx.doi.org/#1}{#1}}% <-- Drop shadow + text for reference number + hyperref
}%
}

\begin{document}
\DOIbox{10.5281/zenodo.241372}
\end{document}

enter image description here

  • That's very pretty! – Andy Clifton Sep 17 at 12:26
  • 1
    @AndyClifton All the hard work was done by tcolorbox developpers – BambOo Sep 17 at 12:48
  • 1
    @AndyClifton I slightly corrected the MWE, adding % at end of lines in the \newcommand without this correction, spaces are added – BambOo Sep 17 at 14:43
  • Nice! Can I make this into a link in the same way as my initial MWE? – Andy Clifton Sep 18 at 20:12
  • 1
    @AndyClifton Just updated a modification – BambOo Sep 19 at 7:38
5

Here's a version that attempts to reproduce the color gradient and font shadow effects of the Zenodo badges. The color gradient is not rendered as well as in the SVG versions, unfortunately.

\documentclass{article}
\usepackage{fontspec}
\usepackage{relsize}
\usepackage{libertine}
\usepackage{calc}
\usepackage{tikz}
\usepackage[margin=1cm]{geometry}
\usetikzlibrary{calc,positioning}

\newlength{\doiLength}

%% shield as per https://zenodo.org/badge/doi/10.5281/zenodo.241372.svg
\newcommand{\DOIShield}[1]{
  \settowidth{\doiLength}{#1}
  \raisebox{-0.6ex}{%
  \begin{tikzpicture}[anchor=base,baseline]
    \node (doi) {\includegraphics[width=1.2\doiLength{}+3em]{doi-shield}};
    \node[right=2em+0.12\doiLength of doi.west,font={\fontspec{DejaVu Sans}\selectfont\smaller}] (n) {#1};
    \node[font={\fontspec{DejaVu Sans}\selectfont\smaller},anchor=south
      west,xshift=0.005em,yshift=0.007em] at (n.south west) {\color{white}{#1}};
  \end{tikzpicture}}}

\begin{document}
Foo \DOIShield{10.5281/zenodo.241372}.

\Huge
Bar \DOIShield{10.1016/0090-2616(77)90044-4}.
\end{document}

Example output: Example DOI badges

where the file doi-shield.pdf is simply a conversion of a Zenodo SVG shield to PDF, with the DOI component deleted.

  • +1 for the shadow, I did not pay attention to that (at first :)) – BambOo Sep 17 at 22:43
  • Thanks for this. Am I right in thinking that because of fontspec it can't be used with PDFlatex? – Andy Clifton Sep 19 at 8:52
  • 1
    The fontspec package is only used to have the same font as the Zenodo badges, DejaVu Sans. If you're using pdflatex, you can use the DejaVuSans package instead. – Eric Marsden Sep 19 at 9:58
3

Add a \strut to before #3 of \fcolorbox so they are the same height.

  • I would still welcome other answers if there is a better approach! – Andy Clifton Sep 16 at 20:42
  • 2
    @AndyClifton in that case I would hold off accepting this answer for a few days, most people will skip over questions with accepted answers. – David Carlisle Sep 16 at 21:14

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.