How to make some text appear with a background like it appears in tags, for example the tags in Stackoverflow?
5 Answers
Here's an alternative solution with Tikz.
Output

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}
- 37,338
-
1Wow, this is so great! I'm borrowing this to use in my CV template :) – Igal Tabachnik Aug 03 '19 at 20:24
-
\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}
-
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
tcolorboxversion, most likely. It works out of the box at least fortcolorboxversion 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
-
-
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
-
-
The updated code has these errors:
! Package pgfkeys Error: I do not know the key '/tcb/halign' and I am going toand same for/tcb/box. – gsamaras May 28 '16 at 13:59 -
@gsamaras: You must have an really outdated
tcolorboxversion then.ubuntupackages 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
tcolorboxis 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-extratoo 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
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}
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.
- 42,268
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
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
- 62,301




\colorboxcomes from the color packge. very reputable author! – David Carlisle May 28 '16 at 13:42/colorbox. I will search him/her though! Thanks for the information @DavidCarlisle, – gsamaras May 28 '16 at 13:43colorboxis ;-) – May 28 '16 at 13:46