如何使某些文本以背景显示,就像在标签中显示的那样,例如 Stackoverflow 中的标签?
答案1
\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}
更新:使用 tikz 样式并替换sharp corners
为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}
答案2
这是 Tikz 的替代解决方案。
输出
代码
\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}
答案3
您可以使用\colorbox{red}{text}
标准乳胶color
包。
答案4
为了好玩,这里有一个 ConTeXt 解决方案。与其他解决方案一样,您只需要一个具有适当背景颜色和文本颜色的彩色框。在 ConTeXt 中,\framed
宏提供了这样的框。因此,我们用正确的参数定义一个新框架:
\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
这使
如果您想要圆角(在移动设备上查看时,标签被圆形框包围),则将定义更改为:
\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,
]
这使