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

costrouc
  • 125

2 Answers2

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

Zarko
  • 296,517
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

Bernard
  • 271,350