10

How to make some text appear with a background like it appears in tags, for example the tags in Stackoverflow?

  • what have you tried? \colorbox, fancybox package, tcolorbox package, tikz, pstricks, ..... – David Carlisle May 28 '16 at 13:32
  • @DavidCarlisle thanks, I searched but I couldn't find anyone having tried what I am trying to do! I will take a look! :) Are you going to post an answer or should I delete the question? – gsamaras May 28 '16 at 13:42
  • 5
    \colorbox comes from the color packge. very reputable author! – David Carlisle May 28 '16 at 13:42
  • 4
    @gsamaras: Guess who the author of colorbox is ;-) – user31729 May 28 '16 at 13:46
  • 1
    Isn't this kind of a repeat? Cool Text Highlighting in LaTeX tex.stackexchange.com/questions/5959/… – A Feldman May 28 '16 at 13:55
11
\documentclass{article}

\usepackage[skins]{tcolorbox}

\newtcolorbox{tagbox}[1][]{colback=blue!20!white,sharp corners,boxrule=0pt,enhanced jigsaw,nobeforeafter,width=3cm,halign=center,valign=center}

\newcommand{\sotagbox}[1]{%
  \begin{tagbox}
    #1
  \end{tagbox}
}

\begin{document}
\sotagbox{foo}
\end{document}

Update: With a tikz style and replacing sharp corners by arc=0pt etc.

\documentclass{article}

\usepackage[skins]{tcolorbox}


\newtcolorbox{tagbox}[1][]{
  colback=blue!20!white,
  arc=0pt,
  auto outer arc,
%  sharp corners,
  boxrule=0pt,
  enhanced jigsaw,
  nobeforeafter,
  width=2cm,
  boxsep=0pt,
  halign=center,
  valign=center,
  box align=center,
  colupper={blue!40!black}, % a dark blue
  fontupper={\bfseries},
  top=\fboxsep,
  bottom=\fboxsep,
  left=\fboxsep,
  right=\fboxsep,
  baseline=\fboxsep,
  #1
}

\newcommand{\sotagbox}[2][]{%
  \begin{tagbox}[#1]%
    #2
  \end{tagbox}%
}

\begin{document}
\sotagbox{foo} versus \colorbox{blue!20!white}{foo} and something with a shadow: \sotagbox[drop shadow]{foo}
\end{document}

enter image description here

  • Error: ! Package pgfkeys Error: I do not know the key '/tcb/sharp corners' and I am go...Am I missing a package or something? – gsamaras May 28 '16 at 13:47
  • @gsamaras: Depends on your tcolorbox version, most likely. It works out of the box at least for tcolorbox version later than 3.12 etc. – user31729 May 28 '16 at 13:49
  • Any idea how to troubleshot that Christian? :) OK, how to see my version? – gsamaras May 28 '16 at 13:50
  • @gsamaras: Updating tcolorbox? ;-) – user31729 May 28 '16 at 13:50
  • OK searching for it. However this code compiles with no problem. Hmm no luck with the updating process so far, I am in Ubuntu, so if you know, let me know :P – gsamaras May 28 '16 at 13:54
16

Here's an alternative solution with Tikz.

Output

enter image description here

Code

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}

\definecolor{backg}{RGB}{225,236,244}
\definecolor{tagtxt}{RGB}{88,115,159}

\newcommand\sotag[1]{%
    \tikz[baseline]{%
        \node[anchor=base, text=tagtxt, fill=backg, font=\sffamily, text depth=.5mm] {#1};
    }%
}

\begin{document}
Here are some tags: \sotag{android}, \sotag{broadcastreceiver}, and \sotag{gallery}
\end{document}
  • Wow, this is so great! I'm borrowing this to use in my CV template :) – Igal Tabachnik Aug 3 at 20:24
  • Thanks Igal! :) – Alenanno Aug 3 at 23:29
8

You could use \colorbox{red}{text} from the standard latex color package.

3

In Context you can do this with \framed and its relations. Here's a simple lualatex + Metapost approach that does something similar.

\documentclass{article}
\usepackage{fontspec}
\usepackage[svgnames]{xcolor}
\setmainfont{TeX Gyre Pagella}
\setsansfont[Scale=MatchLowercase]{TeX Gyre Heros}
\usepackage{luamplib}
\newcommand\ttag[1]{%
    \setbox0\hbox{%
    \begin{mplibcode}
        beginfig(0);picture tt; tt=textext("\phantom(\sffamily #1\phantom)");
        path corner, box; corner = quartercircle scaled 3;
        bboxmargin := 0;
        box = corner rotated   0 shifted urcorner tt
           -- corner rotated  90 shifted ulcorner tt
           -- corner rotated 180 shifted llcorner tt
           -- corner rotated 270 shifted lrcorner tt
           -- cycle;
        fill box withcolor \mpcolor{LightSteelBlue};
        draw tt  withcolor \mpcolor{Navy};
        endfig;
    \end{mplibcode}}\raise\MPlly bp\box0 }% <-- need a space here
\begin{document}

Here are some tags: \ttag{android}, \ttag{broadcast receiver}, and \ttag{gallery}.
To approach true user-friendliness, the interrelation of system and/or
subsystem \ttag{technologies} must utilize and be functionally interwoven with the
preliminary qualification limit.  In particular, any associated supporting element
necessitates that urgent consideration be applied to possible bidirectional logical
relationship approaches.  Conversely, any associated supporting element recognizes
other systems' importance and the \ttag{necessity} for possible bidirectional logical
relationship approaches.  However, a service-oriented para\-digm is further compounded
when taking into account the evolution of specifications \ttag{over} a given time period.  

\end{document}

example output showing the new command

As you can see from the output, there are some limitations. In particular the tags will be set as a horizontal box, so the TeX line-breaking algorithm will not break them. So best used sparingly.

The \phantom( and \phantom) not only provide some visual padding around the tag, but also ensure that the tag boxes are a uniform size regardless of the presence or absence of ascenders and descenders.

If you were happy with square corners, you could just use bbox tt instead of the elaborate box-and-corner construction. If you want larger or smaller corners you should change the scale factor in the definition of corner.

The only clever bit is the part that puts the baseline of the box in the right place. luamplib sets four variables to tell you the size of the box it has created. By saving the box into a box register, we can then raise it by the (negative) depth of the lower left corner to get it in the right place. Note that you have to add the unit bp as well. Details in the luamplib documentation.

3

For fun, here is a ConTeXt solution. As in other solutions, all you need is a colored box with appropriate background color and text color. In ConTeXt, \framed macro provides such boxes. So, we define a new frame with the right parameters:

\usecolors[x11] % To use lightsteelblue and navy colors

\defineframed
  [tagged]
  [
    location=low, % align with text baseline
    foregroundcolor=navy,
    background=color,
    backgroundcolor=lightsteelblue,
    loffset=0.25\lineheight,
    roffset=0.25\lineheight,
    frame=off,
  ]

\starttext

Normal text \tagged{tagged-text} normal text

\stoptext

which gives

enter image description here

If you want round corners (when viewed on mobile, the tags are surrounded by a round box), then change the definition to:

\defineframed
  [tagged]
  [
    location=low,
    foregroundcolor=navy,
    background=color,
    backgroundcolor=lightsteelblue,
    loffset=0.25\lineheight,
    roffset=0.25\lineheight,
    frame=on,
    corner=round,
    radius=0.5\lineheight,
    framecolor=navy,
    rulethickness=1pt,
  ]

which gives

enter image description here

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.