13

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

gsamaras
  • 1,443
  • 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
  • Oh I do not know the author of /colorbox. I will search him/her though! Thanks for the information @DavidCarlisle, – gsamaras May 28 '16 at 13:43
  • 4
    @gsamaras: Guess who the author of colorbox is ;-) –  May 28 '16 at 13:46
  • WOW, the author itself commented! Didn't expect that @ChristianHupfer. Nice work Mr. Carlisle. :) – gsamaras May 28 '16 at 13:48
  • 1
    Isn't this kind of a repeat? Cool Text Highlighting in LaTeX http://tex.stackexchange.com/questions/5959/cool-text-highlighting-in-latex – A Feldman May 28 '16 at 13:55
  • @AFeldman nop. If you want to mark some duplicates, I would suggest this and this. However, please notice that I wasn't able to locate them before David's comment. – gsamaras May 28 '16 at 13:56
  • That happens to me quite a bit. Well, I guess both ChristianHupfer and DavidCarlisle would know better than myself if this is a duplicate or not. – A Feldman May 28 '16 at 14:05

5 Answers5

21

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}
Alenanno
  • 37,338
15
\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. –  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? ;-) –  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
  • @gsamaras: That's with arc=... most likely -- I improved my code! –  May 28 '16 at 13:58
  • The updated code has these errors: ! Package pgfkeys Error: I do not know the key '/tcb/halign' and I am going to and same for /tcb/box. – gsamaras May 28 '16 at 13:59
  • @gsamaras: You must have an really outdated tcolorbox version then. ubuntu packages are a mess, sorry! –  May 28 '16 at 14:00
  • Hmm, I just tried this, but still no luck. I am trying to find another update then! – gsamaras May 28 '16 at 14:03
  • @gsamaras: The latest version of tcolorbox is 3.90 and we soon have TL 2016, so the very latest version is TL 2015 ;-) –  May 28 '16 at 14:04
  • Christian, thanks, but I tried with sudo apt-get install texlive-latex-extra too and it says that is updated. I am not able to run your code, but it seems cool. :) – gsamaras May 28 '16 at 14:08
9

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

David Carlisle
  • 757,742
4

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.

Thruston
  • 42,268
4

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

Aditya
  • 62,301