4

I am trying to align text inside the tikz node like in picture below. I tried to use tabular environment, but I wasn't successful. Is there a way to align text like that?

example

Code I wrote so far:

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicure}
     \tikzstyle{status}=[draw,rectangle,rounded corners,fill=blue,text centered,inner sep=2pt]
     \node[status] (test) {
     \begin{tabular}{lcr}
     Word 1 & $\rightarrow$ & Value 1
     Word 2 & $\rightarrow$ & Value 2
     Word 3 & $\rightarrow$ & Value 3
     Word 4 & $\rightarrow$ & Value 4
     \end{tabular}};
\end{tikzpicture}
\end{document}

Thanks for any help.

EDIT: Code expanded to MWE.

  • 1
    Welcome to TeX.SX! Can you please expand the code snippet that you have posted to a full minimal working example. A MWE should compile and be as small as possible to demonstrate your problem. it's much easier to help you if we have full working code to start from. – Andrew May 1 at 8:36
5

like this?

enter image description here

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                matrix}

\begin{document}
 \begin{tikzpicture}[
 LA/.style = {line width=1mm, -{Triangle[angle=90:1pt 2]}, draw=blue!50!gray},
 BL/.style = {baseline=-0.75ex}
                    ]
\node[draw, rounded corners, fill=blue!30,
      inner ysep=3mm, inner xsep=2mm,
      font=\sffamily\bfseries, align=center,
      label={[font=\sffamily\bfseries,anchor=north west]north west:Example text}
      ]
{\\[2ex]
\renewcommand\arraystretch{1.3}
\begin{tabular}{lcr}
 Word 1 & \tikz[BL]\draw[LA] (0,0) -- ++ (1,0); & Value 1 \\
 Word 2 & \tikz[BL]\draw[LA] (0,0) -- ++ (1,0); & Value 2 \\
 Word 3 & \tikz[BL]\draw[LA] (0,0) -- ++ (1,0); & Value 3 \\
 Word 4 & \tikz[BL]\draw[LA] (0,0) -- ++ (1,0); & Value 4 \\
 \end{tabular}
};
 \end{tikzpicture}
\end{document}
  • Thank you very much. It is exactly what I was looking for. – awarus May 1 at 9:33
  • @awarus, if answer solve your problem on the best way, than you can accept it ... (by clicking on check mart at the top left side of answer) :-) – Zarko May 1 at 11:19
5

It is not clear to me what your ultimate goal is but I recommend against putting tabular environments inside \node's as there are usually better ways to do this. Of course, this really depends on what you are trying to do.

This said, if you do want to do this then you need to put a valid tabular environment inside the node. In particular, you need to end each row of the environment with \\. If you do this then your code produces what you want:

enter image description here

Here is the corrected code (which I have also expanded to a full minimal working example):

\documentclass{article}
\usepackage{tikz}
\begin{document}

  \begin{tikzpicture}
     \tikzstyle{status}=[draw,rectangle,rounded corners,fill=blue,text centered,inner sep=2pt]
     \node[status] (test) {
     \begin{tabular}{lcr}
     Word 1 & $\rightarrow$ & Value 1\\
     Word 2 & $\rightarrow$ & Value 2\\
     Word 3 & $\rightarrow$ & Value 3\\
     Word 4 & $\rightarrow$ & Value 4
     \end{tabular}};
  \end{tikzpicture}

\end{document}

Edit

Taking inspiration from Zarko for the arrows, this is how I would probably do this.

\documentclass[svgnames]{article}
\usepackage{tikz}
\usetikzlibrary{fit, arrows.meta, backgrounds}
\begin{document}

  \begin{tikzpicture}[
        LA/.style={line width=1mm, -{Triangle[angle=90:1pt 2]}, draw=blue!50!gray},
        box/.style={rounded corners,fill=blue!30,text centered,inner sep=2pt, draw=MidnightBlue}]
     \foreach \word/\val [count=\c] in {Word 1/Value 1,Word 2/Value 2,Word 3/Value 3,Word 4/Value 4} {
       \node(word\c) at (0,-0.6*\c) {\word};
       \node(val\c) at (2.4,-0.6*\c) {\val};
       \draw[LA] (word\c)--(val\c);
     }
     \begin{scope}[on background layer]
       \node[box, fit=(word1) (val4)]{};
     \end{scope}
  \end{tikzpicture}

\end{document}

to produce:

enter image description here

  • Thank you very much for answer. It was really helpful for me. – awarus May 1 at 9:34
  • 2
    +1 for not nesting tikzpictures, which is a real improvement IMHO. – user121799 May 1 at 14:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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