17

I'm trying to create stackoverflow like tags. Currently I'm using an \fbox{text} to create the tags, which works, but not all boxes have the same height (it seems that letters like 'p' make a box take more height).

Is there a way to create something like \fbox but with a fixed height?

(They don't need to have a drop shadow or something like that. Just simple framed boxes)

4 Answers 4

28

You should add a \strut so that all boxes have the same maximal height and depth, otherwise the framed box will depend on the height of the content. This means that an a gets a small box and Xy gets a higher and deeper one. With \strut a invisible rule is added which has the maximum height and depth of the current font size.

\newcommand*{\tag}[1]{\fbox{#1\strut}}

Fancier boxes can be created using the fancybox package.

2
  • @percusse: I also work on a tikzbox package which allows for easy usage of TikZ decorations around a text. It will be part of the adjustbox bundle. Nov 16, 2011 at 15:27
  • I am aware of that work of yours and actually trying to get some affinity to it. What I wanted to say is that your last sentence was quite funny to me, but nevermind. Maybe I should refrain myself from putting such comments. Thanks again.
    – percusse
    Nov 16, 2011 at 15:30
13

It might be simplest (although possibly overkill) to do this with tikz:

\documentclass{article}
\usepackage{tikz}
\newcommand{\mytag}[1]{
\begin{tikzpicture}[baseline={(TAG.base)}]
  \node[draw, minimum height=4ex] (TAG) {#1};
\end{tikzpicture}}

\begin{document}
\mytag{a tag}\mytag{Another Tag}
\end{document}

output of code

It's overkill because the \strut solution is simpler. It's not overkill if you want to make the boxes fancier than the fancybox package can give you. A fancier version of the command would allow you to specify more parameters to the node command:

\usetikzlibrary{shapes.geometric}

\newcommand{\mytag}[2][]{\begin{tikzpicture}[baseline={(TAG.base)}]
\node[draw, minimum height=4ex,#1] (TAG) {#2};
\end{tikzpicture}
}

\mytag[fill=red!40, very thick,double, rounded corners]{a tag}

\mytag[ellipse,draw,blue]{Another Tag}

output of code

5
  • Just add a \rule{0pt}{<min height>} instead, or a \strut which is basically a special form of that. Nov 16, 2011 at 14:52
  • 1
    Also you might want to keep the original baseline: \begin{tikzpicture}[baseline={(TAG.base)}] \node[draw, minimum height=4ex] (TAG) {#1}; \end{tikzpicture} Nov 16, 2011 at 14:56
  • 1
    @MartinScharrer Thanks. I agree about the \strut solution, which is clearly the simplest, so I've added some ideas about what else could be done with tikz in this context. I've also added the baseline correction.
    – Alan Munn
    Nov 16, 2011 at 20:16
  • @MartinScharrer: A \strut has a depth, but \rule doesn't. Nov 16, 2011 at 21:41
  • @MartinSchröder: Usually you want a depth here. \rule can have a depth if you use the optional argument. \strut is equivalent to \rule[-.3\baselineskip]{0pt}{\baselineskip}. However, it is implemented differently. Nov 16, 2011 at 21:48
5

I'm not sure you need any fancy stuff for the tag frame or whether you want to add some icons (like in the actual badges), but in case you do, you can use the mdframed package:

\documentclass[12pt,a4paper]{article}

\usepackage[framemethod=TikZ]{mdframed}

\begin{document}
\global\mdfdefinestyle{tag}{%
innerlinewidth=2pt,innerlinecolor=red,%
middlelinewidth=3pt,middlelinecolor=white,%
outerlinewidth=2pt,outerlinecolor=blue,%
}
\begin{mdframed}[userdefinedwidth=4cm,style=tag]
this is my tag
\end{mdframed}
\end{document}

This is what the output will be:

enter image description here

As a note I'd add that this is also based on TikZ (see the global option framemethod=TikZ in the preamble when you load the package) and also, the width of the box has to be set manually in the example, but I'm sure a method to automate this exists.

EDIT: I just couldn't leave it like that and had to fiddle with my code to produce an extended version that recreates the badges on our beloved site. :) The style is still created with mdframed and the medal is drawn with TikZ. The 'badge' takes two arguments: metal and badge text. Enjoy! :)

\documentclass[12pt,a4paper]{article}

\usepackage{tikz}
\usepackage[framemethod=TikZ]{mdframed}

\global\mdfdefinestyle{tag}{%
innerlinewidth=1pt,innerlinecolor=black,%
middlelinewidth=2pt,middlelinecolor=white,%
outerlinewidth=1pt,outerlinecolor=black,%
backgroundcolor=black,fontcolor=white,%
}

\colorlet{gold}{yellow}
\colorlet{silver}{gray!20}
\colorlet{bronze}{orange}

\newcommand{\badge}[2]{%
\begin{mdframed}[userdefinedwidth=5.5cm,style=tag]
\tikz{\draw[very thin,gray!75,fill=#1] (0,0) circle (1ex);\pgfmathsetmacro{\triangleside}{sqrt(3)}\draw[very thin,gray!75] (0,1ex) -- ++(240:\triangleside ex) -- +(0:\triangleside ex) -- cycle;} #2
\end{mdframed}
}

\begin{document}
\badge{gold}{This is my \TeX\ badge}
\badge{silver}{This is my \TeX\ badge}
\badge{bronze}{This is my \TeX\ badge}
\end{document} 

enter image description here

2
  • Very nice output, congrats! :) Nov 20, 2011 at 10:23
  • @PauloCereda: Thanks! :) I really enjoyed tinkering around with this one and now I feel it paid off!
    – Count Zero
    Nov 20, 2011 at 16:00
1

You don't need drop shadow or something like that, but you could use/misapply keystroke

MWE:

\documentclass{scrartcl}
\usepackage{keystroke}

\begin{document}

\keystroke{aa}
\keystroke{AA}
\keystroke{p}

\end{document}

Result: enter image description here

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .