复制 shields.io 徽章

复制 shields.io 徽章

我正在尝试复制 Zenodo、Github 等中使用的盾牌,以显示 DOI、项目状态和其他信息片段。这些徽章实际上由shields.io,如下所示:

在此处输入图片描述

不幸的是,据我所知,shields.io来自 LaTeX,因此有必要进行一些破解。

我的方法是将两者楔在一起\tcolorboxes

到目前为止我已经编码了这个:

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

目前的结果如下:

在此处输入图片描述

已经很接近了,但还不够。我知道如何修改文本和框的颜色,但框的垂直尺寸不对。如何更改框的高度,使它们相同?

(为了增加业力,有没有更好的方法来创建不涉及 Tikz 的徽章,这让我很害怕?)

编辑:shields.io 实现是一个可点击的图形,链接到 DOI 解析器。我已将其包含在 MWE 中,并且需要一个也实现它的解决方案。

答案1

另一个想法(尚未打包成命令)是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}

在此处输入图片描述

编辑:改进版本,可自动计算盒子尺寸、颜色阴影和\newcommand清晰度

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

在此处输入图片描述

编辑 2 号(希望是最后一个)添加阴影(感谢@Eric Marsden 的想法和动力)。

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

在此处输入图片描述

编辑第 3 条添加hyperref选项hidelinks以使框可点击,并稍微修改阴影设置以减少阴影占用空间

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

在此处输入图片描述

答案2

这个版本试图重现 Zenodo 徽章的颜色渐变和字体阴影效果。遗憾的是,颜色渐变的渲染效果不如 SVG 版本。

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

示例输出: DOI 徽章示例

其中文件 doi-shield.pdf 只是 Zenodo SVG 屏蔽到 PDF 的转换,其中删除了 DOI 组件。

答案3

\strut在之前添加 a#3\fcolorbox使它们具有相同的高度。

相关内容