8

I am trying to add a centrally aligned text in the middle of each horizontal bar, but can't seem to figure out how. How can I best do that?

 \documentclass{article}

    \usepackage{xcolor}
    \usepackage{tikz}

    \definecolor{high}{rgb}{0.89, 0.26, 0.2}
    \definecolor{med}{rgb}{1.0, 0.65, 0.0}
    \definecolor{low}{rgb}{0.98, 0.93, 0.36}

    \begin{document}
    \begin{figure}
    \centering
    \begin{tikzpicture}
    \foreach  \l/\x/\c[count=\y] in {A/6/high, B/4/med, C/2/low}
    {\node[left] at (0,\y) {\l};
    \draw[fill=\c] (0,\y-.4) rectangle (\x,\y+.4);}
    \end{tikzpicture}
    \end{figure}
    \end{document}

enter image description here

6

You just have to add a node at \x/2:

enter image description here

\documentclass{article}

% \usepackage{xcolor}% TikZ already loads xcolor
\usepackage{tikz}

\definecolor{high}{rgb}{0.89, 0.26, 0.2}
\definecolor{med}{rgb}{1.0, 0.65, 0.0}
\definecolor{low}{rgb}{0.98, 0.93, 0.36}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\foreach  \l/\x/\c/\stuff[count=\y] in {%
  A/6/high/text for everyone!,
  B/4/med/more text,
  C/2/low/some text}
{%
  \node [left] at (0,\y) {\l};
  \draw [fill=\c] (0,\y-.4) rectangle (\x,\y+.4);
  \node [anchor=center] at (\x/2,\y) {\stuff};
}
\end{tikzpicture}
\end{figure}
\end{document}
| improve this answer | |
6

Tikz defines a rectangle by two vertices of one of its diagonals, just place the node midway.

By adding a node in the middle of the path that traces the rectangle, we find exactly its center:

rectangle-midway

\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\draw(0,0)rectangle(4,2)node[midway,blue]{S};
\draw[densely dotted, red](0,0)--(4,2);
\draw[densely dotted,red](0,2)--(4,0);
\end{tikzpicture}
\end{document}

Your code becomes:

 \documentclass{article}

    \usepackage{xcolor}
    \usepackage{tikz}

    \definecolor{high}{rgb}{0.89, 0.26, 0.2}
    \definecolor{med}{rgb}{1.0, 0.65, 0.0}
    \definecolor{low}{rgb}{0.98, 0.93, 0.36}

    \begin{document}
    \begin{figure}
    \centering
    \begin{tikzpicture}
    \foreach  \l/\x/\c[count=\y] in {A/6/high, B/4/med, C/2/low}
    {\node[left] at (0,\y) {\l};
    \draw[fill=\c] (0,\y-.4) rectangle (\x,\y+.4)node[midway]{\c};}
    \end{tikzpicture}
    \end{figure}
    \end{document}

the result is:

rectangle-midway

| improve this answer | |
5

Another way :

\documentclass{article}

\usepackage{xcolor}
\usepackage{tikz}
\usetikzlibrary{positioning}

\definecolor{high}{rgb}{0.89, 0.26, 0.2}
\definecolor{med}{rgb}{1.0, 0.65, 0.0}
\definecolor{low}{rgb}{0.98, 0.93, 0.36}

\begin{document}
\begin{figure}
  \centering
  \begin{tikzpicture}
    \foreach  \l/\x/\c[count=\y] in {A/6cm/high, B/4cm/med, C/2cm/low}
    {
      \node[name=left-\l] at (0,\y) {\l};
      \node[fill=\c, minimum width=\x, right=(.2cm of left-\l)] at (0,\y) {\c};
    }
    \end{tikzpicture}
  \end{figure}
\end{document}

enter image description here

| improve this answer | |
5

similarly to flav answer:

\documentclass{article}
\usepackage{xcolor}
\usepackage{tikz}

\definecolor{high}{rgb}{0.89, 0.26, 0.2}
\definecolor{med}{rgb}{1.0, 0.65, 0.0}
\definecolor{low}{rgb}{0.98, 0.93, 0.36}

\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\foreach  \l/\x/\c/\t [count=\y] in {A/6/high/tikz, B/4/med/love, C/2/low/I}
{\node[draw,
       fill=\c,
       label=left:\l,
       text width=\x cm,
       align=center, inner xsep=0pt, minimum height=8mm, right] 
       at (0,\y) {\t};
}
\end{tikzpicture}
\end{figure}
\end{document}

enter image description here

| improve this answer | |

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.