2

I am trying to create a plot with tikz that has several placed rectangles with text wrapped inside of them. I have looked at several examples online and for some reason tikz is ignoring my manual line breaks. Why is this?

If there is a better way to wrap text inside a rectangle please let me know.

I have tried to put align=center in node[Smokey, opacity=1] and it did nothing. Recommended from TikZ/pgf: Why does `\newline` and `\\` not split text in node?

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}

\definecolor{Smokey}{RGB}{88, 89, 91}
\definecolor{TennesseeOrange}{RGB}{255, 130, 0}

\begin{document}

\begin{tikzpicture}[font=\small]
  \begin{axis}[%
      scale only axis,
      axis lines=middle,
      axis line style={->},
      xmin=0, xmax=8,
      xtick={1, 2, 3, 4, 5, 6, 7},
      xticklabel style={text height=2ex},
      xticklabels={$ps$,$ns$,$\mu s$,$ms$,$s$, $hours$, $years$},
      ymin=0,
      ymax=5,
      ytick={1, 2, 3, 4},
      yticklabels={$nm$, $\mu m$, $mm$, $m$}]
    \draw [TennesseeOrange, thick,rounded corners, fill=TennesseeOrange, fill opacity=0.2]
    (axis cs:.1,.1) rectangle node[Smokey, opacity=1]{I ignore newlines \\ why is \\ this \\ I don't know?} (axis cs:3,3);
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • 3
    ,align=center -> \draw [TennesseeOrange, thick,rounded corners, fill=TennesseeOrange, fill opacity=0.2,align=center] (axis cs:.1,.1) rectangle node[Smokey, opacity=1]{I ignore newlines \\ why is \\ this \\ I don't know?} (axis cs:3,3); should help first. Related links, TikZ/pgf: Why does \newline and `\` not split text in node? and Manual/automatic line breaks and text alignment in TikZ nodes – Bobyandbob Jan 27 '18 at 0:10
  • Thank you I know exactly what I did wrong. I put align=center in the node[Smokey, opacity=1] and it did nothing so I thought those posts you referenced were of no help. It should have been put in the original draw statement. Personally I think this is a gotcha - would be happy to accept your answer as it did exactly what I needed. – costrouc Jan 27 '18 at 0:15
  • or just use text width key – percusse Jan 27 '18 at 0:24
3

to node's options let add align=center:

\documentclass{article}
\usepackage{pgfplots}
\usepackage{siunitx}
\definecolor{Smokey}{RGB}{88, 89, 91}
\definecolor{TennesseeOrange}{RGB}{255, 130, 0}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[%
      scale only axis,
      axis lines=middle,
      axis line style={->},
      xmin=0, xmax=8,
      xtick={1, 2, 3, 4, 5, 6, 7},
      xticklabel style={text height=2ex},
      xticklabels={\si{\ps},\si{\ns},\si{\us},\si{\us},\si{\s}, \si{\hour} , year},
      ymin=0,
      ymax=5,
      ytick={1, 2, 3, 4},
      yticklabels={\si{\nm}, \si{\um}, \si{\mm}, \si{\m}}]
    \draw [TennesseeOrange, thick, rounded corners, fill=TennesseeOrange, fill opacity=0.2]
    (axis cs:.1,.1) rectangle node[Smokey, opacity=1,align=center]{I don't\\ ignore\\ newlines} (axis cs:3,3);
  \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

1

Use the stackengine package, for instance, and (unrelated to your question) siunitx, to have a correct display of units:

\documentclass{article}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[usestackEOL]{stackengine}
\usepackage{siunitx}
\definecolor{Smokey}{RGB}{88, 89, 91}
\definecolor{TennesseeOrange}{RGB}{255, 130, 0}

\begin{document}

\begin{tikzpicture}[font=\small]
  \begin{axis}[%
      scale only axis,
      axis lines=middle,
      axis line style={->},
      xmin=0, xmax=8,
      xtick={1, 2, 3, 4, 5, 6, 7},
      xticklabel style={text height=2ex},
      xticklabels={\si{\ps},\si{\ns},\si{\us},\si{\us},\si{\s}, \si{\hour} , year},
      ymin=0,
      ymax=5,
      ytick={1, 2, 3, 4},
      yticklabels={\si{\nm}, \si{\um}, \si{\mm}, \si{\m}}]
    \draw [TennesseeOrange, thick,rounded corners, fill=TennesseeOrange, fill opacity=0.2]
    (axis cs:.1,.1) rectangle node[Smokey, opacity=1]{\Centerstack{I ignore newlines \\ why is \\ this \\ I don't know?}} (axis cs:3,3);
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

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.