First, as David Carlisle said, you should not use \bf
in LaTeX (plus, it doesn't take an argument, it is what Leslie Lamport would call a declaration: it acts on whatever follows in the current TeX group). I replaced it with font=\normalfont\bfseries
in the node options.
You have texts of different depths, but you presumably want the black rectangles to have exactly the same height. Since you said in a comment that you would like vertical placement to be the same as for Levels
, which has no descender, I propose to ignore the depth of the texts passed to your \textbox
macro. This can be done either with TikZ options or with box manipulation commands provided by (La)TeX.
TikZy ways
Using TikZ nodes for the black rectangles
Here, we use a TikZ node with rectangle
shape to draw each text as well as the black rectangle behind it (in your solution, the black rectangles were drawn with a rectangle
operation, not a node). For cleanliness and readability of the code, we define a TikZ style named my special node
that contains all options needed for these special nodes. The most important options in this style in relation to your question are:
text depth=0pt
in order to ignore the depth of the node contents when computing its dimensions;
anchor=south west
to place the node by giving the coordinates of its lower left corner;
font=\normalfont\bfseries
to select the font;
minimum width
and minimum height
so that the rectangles are at least as wide and high as mandated by the arguments of your \textbox
macro;
inner sep=0pt
to ensure that no more space is automatically added inside the node, around its contents (with a larger value, the node contents could “push” its border even when not touching it; this can be desirable in some way, but may also easily result in nodes with different heights if the contents of a node is higher than that of other nodes) .
You don't really need line width=0.3mm
(you could use 0pt
) nor the -0.5\pgflinewidth
in the xshift
and yshift
amounts used for the node. The only reason I include them is to provide an exact match with your example (for the line width
) and with the TeXnical method given below. With the following code, the TikZ calc
and positioning
libraries are not needed at all.
\documentclass{article}
\usepackage[papersize={85mm, 15mm}]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\pagestyle{empty}
\tikzset{
my special node/.style n args={4}{
line width=0.3mm, draw=black, fill=black, text=white,
inner sep=0pt, text depth=0pt, anchor=south west, font=\normalfont\bfseries,
minimum width={(#3)-(#1)}, minimum height={(#4)-(#2)},
}
}
\begin{document}
\newcommand{\textbox}[5]{%
\begin{tikzpicture}[remember picture, overlay]
\node[my special node={#1}{#2}{#3}{#4}]
at ([xshift={(#1)-0.5\pgflinewidth}, yshift={(#2)-0.5\pgflinewidth}]%
current page.south west) {#5};
\end{tikzpicture}%
}
\textbox{10.0mm}{5mm}{40.0mm}{10.0mm}{Levels}
\textbox{45.0mm}{5mm}{75.0mm}{10.0mm}{Gauging}
\end{document}

Cropped screenshot:

Proof that both texts have the same baseline:

One nice thing when your rectangles are TikZ nodes is that you can use many TikZ tools to work with them: name nodes, place nodes relatively to other ones, connect nodes, drop shadows, etc. For instance, with the above code, you only need to add \usetikzlibrary{shadows}
and the drop shadow
node option to obtain this:

Using the rectangle
operation
@Schrödinger'scat proposed another nice TikZy solution that is very close to your code and produces the exact same output as my two methods given above and below. This method uses the rectangle
operation to draw the black rectangles, as opposed to filling the rectangle nodes containing the texts from the fifth argument of \textbox
.
\documentclass{article}
\usepackage[papersize={85mm, 15mm}]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\pagestyle{empty}
\begin{document}
\newcommand*{\textbox}[5]{%
\begin{tikzpicture}[remember picture, overlay]
\filldraw [fill=black, draw=black, line width=0.3mm]
(current page.south west)+(#1,#2) rectangle +(#3,#4)
node[pos=0.5, font=\normalfont\bfseries, text depth=0pt, text=white] {#5};
\end{tikzpicture}%
}
\textbox{10.0mm}{5mm}{40.0mm}{10.0mm}{Levels}
\textbox{45.0mm}{5mm}{75.0mm}{10.0mm}{Gauging}
\end{document}
The output is exactly the same as above.
TeXnical way
Here, instead of using text depth=0pt
for the nodes containing the boxed texts, we use a short macro that puts its argument in a box, sets the box depth to zero and outputs the box. This implies that again, TeX will ignore the depth of the argument when determining the vertical extent of the TikZ node contents.
\documentclass{article}
\usepackage[papersize={85mm, 15mm}]{geometry}
\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{calc}
\pagestyle{empty}
\newsavebox{\mybox}
\newcommand*{\killboxdepth}[1]{%
\sbox{\mybox}{#1}%
\dp\mybox=0pt \box\mybox
}
\begin{document}
\newcommand*{\textbox}[5]{%
\begin{tikzpicture}[remember picture, overlay]
\filldraw [fill=black, draw=black, line width=0.3mm]
($(current page.south west)+(#1,#2)$) rectangle
($(current page.south west)+(#3,#4)$)
node[pos=0.5, font=\normalfont\bfseries] {\color{white}\killboxdepth{#5}};
\end{tikzpicture}%
}
\textbox{10.0mm}{5mm}{40.0mm}{10.0mm}{Levels}
\textbox{45.0mm}{5mm}{75.0mm}{10.0mm}{Gauging}
\end{document}
The output is exactly the same as in the TikZy ways.
Note: another way to define \killboxdepth
is the following:
\newcommand{\killboxdepth}{%
\raisebox{0pt}[\height][0pt]%
}
This is a bit more LaTeXish than using the \dp
and \box
commands, which are TeX primitives. As @barbarabeeton pointed out, yet another way—assuming amsmath
is loaded—would be to use \smash[b]
instead of \killboxdepth
(then this macro isn't necessary). There is definitely more than one way to do it!
\bf
is not defined by default in latex, and when it is defined (for compatibility with documents written in the 1980s) the syntax is{\bf Levels}
not\bf{Levels}
use\textbf{levels}
\strut#5
so they always aligned as if they had a descender\strut%5
then they align. However, both of the words are shifted up. I think it looks more natural if it is placed like the word "Levels". So in case of "Gauging", the g's should be sticking out to the bottom.\strut
approach, here is the related question: tex.stackexchange.com/questions/133227/…