Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Consider the following code:

\begin{tikzpicture}
\node[align=left,font={\small\bfseries}] at (-30 pt, 30pt) {99.99\%        $500 \mu s$};
\node[align=left,font={\small\bfseries}] at (-30 pt, 10pt) {Median Latency $5 \mu s$};
\end{tikzpicture}

How can I make the 500 us align with the 5 us in the rendered image?

More specifically, I want it to align almost exactly like I have it in the plain text in the code above, with the left edge of 99 aligning with the left edge of Median, as well as having the numbers line up.

I tried using & as I would when trying to align two equations, but it just causes an error.

share|improve this question
    
What about using tabular? –  egreg Jun 4 at 20:51

4 Answers 4

up vote 3 down vote accepted

First solution

Using your code sample, you can use right or anchor=west to align both nodes to the left together with a makebox to make sure ensure the left part of the text takes the same space in both nodes and are vertically aligned (see @egreg comment below):

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \node[font={\small\bfseries},left] at (-30 pt, 30pt) {\parbox{3cm}{99.99\%}$500 \mu s$};
  \node[font={\small\bfseries},left] at (-30 pt, 10pt) {\parbox{3cm}{Median Latency}$5 \mu s$};
\end{tikzpicture}
\end{document}

Output

Second solution

Or you could use another node for the right part of the text (but both texts are not vertically aligned):

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
  \draw (-30 pt, 30pt) node[font={\small\bfseries},left] {99.99\%} ++(3,0) node[left] {$500 \mu s$};
  \draw (-30 pt, 10pt) node[font={\small\bfseries},left] {Median Latency} ++(3,0) node[left] {$5 \mu s$};
\end{tikzpicture}
\end{document}

Second solution output

share|improve this answer
1  
The horizontal alignment is good, the vertical is bad. –  egreg Jun 4 at 20:50
    
Indeed! Then I guess, I could specify the height of the parbox but It wouldn't be very clean. –  remjg Jun 4 at 20:53
1  
Use \makebox[3cm][l]{99.9\%}, not \parbox –  egreg Jun 4 at 20:56

If you need to place them in two separate nodes I would recommend using a \makebox:

enter image description here

Notes:

  • I would also highly recommend you use the siunitx package for units to ensure that they are correctly and consistently formatted as I did in this example.

Code:

\documentclass[varwidth,margin=0.5cm]{standalone}
\usepackage{tikz}
\usepackage{calc}
\usepackage{siunitx}

\newcommand*{\Widest}{\small\bfseries Median Latency}
\newcommand*{\MakeBox}[2]{\makebox[\widthof{\Widest}][l]{#1} #2}

\begin{document}
\begin{tikzpicture}
  \node[font={\small\bfseries},anchor=west] at (-30 pt, 30pt) {\MakeBox{99.99\%}{$\SI{500}{\micro \second}$}};
  \node[font={\small\bfseries},anchor=west] at (-30 pt, 10pt) {\MakeBox{Median Latency}{$\SI{5}{\micro \second}$}};
\end{tikzpicture}

\end{document}
share|improve this answer

You could use a matrix of nodes, or use a multipart node, or -- depending on what you really want to do -- just place a tabular in a node. (Of course, as egreg hints at with his comment, if this isn't part of a larger tikzpicture just use a tabular and drop the tikzpicture altogether.)

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\section*{With matrix}
\begin{tikzpicture}

\matrix (m) [matrix of nodes,column 1/.style={font={\small\bfseries},text width=3cm,align=left},
column 2/.style={text width=1cm,align=left}] {
99.99\% & $500\mu s$ \\
Median Latency & $5\mu s$ \\
};

% to demonstrate that individual cells can be referenced
\draw [red,latex-] (m-1-2) -- ++(0,1);

\end{tikzpicture}

\section*{Tabular in node}
\begin{tikzpicture}

\node {%
\begin{tabular}{ll}
\small\bfseries 99.99\% & $500\mu s$ \\
\small\bfseries Median Latency & $5\mu s$
\end{tabular}
};

\end{tikzpicture}

\section*{Multipart node}

\begin{tikzpicture}[splitnode/.style={rectangle split,rectangle split horizontal,rectangle split parts=2,rectangle split part align={left},text width=3cm,font={\small\bfseries}}]

\node[splitnode] at (-30 pt, 30pt) {99.99\%   \nodepart{two}$500 \mu s$};
\node[splitnode] at (-30 pt, 10pt) {Median Latency \nodepart{two}$5 \mu s$};
\end{tikzpicture}
\end{document}
share|improve this answer

Maybe an old way of doing it in the sense that I haven't seen the command \shortstack around for many years. You can change the separation of both lines modifying the dimension in \vspace and if you don't like the 500 entering above the Latency you can add some \hspace before the $5\mu s. However, I should not align the 5 hundred with the 5 (unit), but the units with the units.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\tikz \node[font={\small\bfseries}] {\shortstack{%
                       99.99\%\hfill $500 \mu s$\\ 
                       \vspace{1ex}             \\
                       Median Latency $5 \mu s$}};

\end{document}

As @egreg has suggested, tabular is probably the best way of doing it but if you want less separation between both lines, you must add negative \vspacein the middle row.

\documentclass{article}

\usepackage{tikz}

\begin{document}

\tikz \node[font={\small\bfseries}] {%
        \begin{tabular}{ll}%{l@{}r}
          99.99\%        & $500 \mu s$ \\
          \vspace{0pt}   &             \\
          Median Latency & $5   \mu s$
        \end{tabular}};

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.