更改 tikzpicture 中文本和图形的位置

更改 tikzpicture 中文本和图形的位置

假设我有类似的东西:

\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}

产生如下结果

在此处输入图片描述

我有一个灰色框,左侧有一个图形(带有红线、一些文本,白色作为填充色)和一个文本块。现在,如果我想将图形放在该灰色框的右侧,使其与灰色框右侧的距离与现在水平方向与灰色框左侧的距离完全相同,如果我想对文本块执行相同操作(将文本块放在该灰色框的左侧,使其与灰色框左侧的距离与现在水平方向与灰色框右侧的距离完全相同),我该怎么做?

答案1

正如 Sigur 所建议的,您可以使用scopes来移动符号和文本:

\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}

在此处输入图片描述

第二版本:

尽管我更愿意利用nodes他们的positioning能力来做这件事。

该标志有regular polygon3 个面。Hello并且Goodbye可以添加label=center:...

text只是一个可以放置above/below rightabove/below left来自标志的常规节点。

灰色背景可以在两个节点之后绘制。借助backgroundtikzlibrary,您可以有不同的选择: - show background rectangle - 背景层上的填充矩形 -背景层上的填充fitting节点(需要tikzlibrary)fit

下一个代码显示了我如何做到的。第二张图片的变化below leftbelow right。就这样。

\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}

在此处输入图片描述

相关内容