[Tex/LaTex] Changing the positions of text and figures in tikzpicture

tikz-pgf

Suppose that I have something like:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\noindent
\begin{tikzpicture}
\draw[fill=black!40, draw = black!40, line width=2pt]
    (-2, -2.5) rectangle +(\textwidth, 4cm);
\draw (2, -0.3) node[right, text
    width=\textwidth-4.5cm] {This is a test as you can see and we put more text to see what happens here and there.};
\draw[red, fill=white, rounded corners = 5pt, line width=10pt]
    (30:2cm) -- (150:2cm) -- (270:2cm) -- cycle;
\draw (0, 0.3) node {Hello};
\draw (0, -0.3) node {Goodbye};
\end{tikzpicture}
\end{document}

which produces something like this

enter image description here

I have a grayish box, a figure on the left (with red line, some text, white as filling color) and one block of text. Now if I wish to put the figure on the right hand side of that gray box so it will be exactly the same distance from right hand side of the gray box that it is now horizontally from the left hand side of the gray box, and if I wish to the same thing with the text block (put the text block on the left hand side of that gray box so it will be exactly the same distance from the left hand side of the gray box that it is now horizontally from the right hand side of the gray box), what I have to do?

Best Answer

As Sigur suggested, you can use scopes to shift the sign and the text:

\documentclass{article}
\usepackage{tikz}
\begin{document}
\noindent
\begin{tikzpicture}
\draw[fill=black!40, draw = black!40, line width=2pt]
    (-2, -2.5) rectangle +(\textwidth, 4cm);
\draw (2, -0.3) node[draw, right, text
    width=\textwidth-4.5cm] {This is a test as you can see and we put more text to see what happens here and there.};
\draw[red, fill=white, rounded corners = 5pt, line width=10pt]
    (30:2cm) -- (150:2cm) -- (270:2cm) -- cycle;
\draw (0, 0.3) node {Hello};
\draw (0, -0.3) node {Goodbye};
\end{tikzpicture}

\noindent
\begin{tikzpicture}
\draw[fill=black!40, draw = black!40, line width=2pt]
    (-2, -2.5) rectangle +(\textwidth, 4cm);
    \begin{scope}[xshift=-3.8cm]
\draw (2, -0.3) node[draw, right, text
    width=\textwidth-4.5cm] {This is a test as you can see and we put more text to see what happens here and there.};
    \end{scope}
    \begin{scope}[xshift=\textwidth-4cm]
\draw[red, fill=white, rounded corners = 5pt, line width=10pt]
    (30:2cm) -- (150:2cm) -- (270:2cm) -- cycle;
\draw (0, 0.3) node {Hello};
\draw (0, -0.3) node {Goodbye};
\end{scope}
\end{tikzpicture}

\end{document}

enter image description here

2nd version:

Although I'd prefer to do it with nodes and their positioning capabilities.

The sign is a regular polygon with 3 sides. Hello and Goodbye can be added to it with a label=center:....

The text is just a regular node which can be placed above/below right or above/below left from the sign.

And the gray background can be drawn after both nodes. You have different options, thanks to background tikzlibrary: - show background rectangle - a filled rectangle on background layer - filled fitting node (needs fit tikzlibrary) on background layer

Next code shows how I did it. Second picture changes below left by below right. That's all.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric,positioning,backgrounds}

\begin{document}
\noindent
\begin{tikzpicture}[background rectangle/.style={fill=black!40, 
                                    inner frame sep=3pt},
                    show background rectangle]
\node[regular polygon, regular polygon sides=3, line width=10pt, rounded corners,
      minimum size=3.6cm+\pgflinewidth, draw=red, fill=white, 
      shape border rotate=60, 
      label={[text width=1.5cm,align=center]center:{Hello\\Goodbye}}] 
      (sign) at (0,0){};

\node[below right=-2mm and 2cm of sign.center, 
      text width=\textwidth-4.5cm] (text) 
      {This is a test as you can see and we put more 
          text to see what happens here and there.};

\end{tikzpicture}

\noindent
\begin{tikzpicture}[background rectangle/.style={fill=black!40, 
                                    inner frame sep=3pt},
                    show background rectangle]
\node[regular polygon, regular polygon sides=3, line width=10pt, rounded corners,
      minimum size=3.6cm+\pgflinewidth, draw=red, fill=white, 
      shape border rotate=60, 
      label={[text width=1.5cm,align=center]center:{Hello\\Goodbye}}] 
      (sign) at (0,0){};

\node[below left=-2mm and 2cm of sign.center, 
      text width=\textwidth-4.5cm] (text) 
      {This is a test as you can see and we put more 
          text to see what happens here and there.};

\end{tikzpicture}
\end{document}

enter image description here