如何将节点放在准确的位置?如何在 TikZ 中使用不同风格的字体?

如何将节点放在准确的位置?如何在 TikZ 中使用不同风格的字体?

我想要画这样的东西:

在此处输入图片描述

这是我的代码:

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
%\usepackage[active,tightpage,floats]{preview}
%\setlength\PreviewBorder{30pt}%
\usetikzlibrary{calc, fit, shapes.geometric}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}

%\colorlet{MAROON}{Maroon}

\begin{document}

\begin{figure}[ht]
  \centering
  \begin{tikzpicture}
    \coordinate (Origin)   at (0,0);

    \coordinate (XAxisMax) at (5.5,0);
    \coordinate (YAxisMax) at (0,4.5);

    \coordinate (Ylow) at (-0.5,0);
    \coordinate (Xlow) at (0,-0.5);
    \coordinate (XMax) at (5.5,-0.5);
    \coordinate (YMax) at (-0.5,4.5);

    \draw [thick, darkgray] (Origin) -- (XAxisMax);% Draw x axis
    \draw [thick, darkgray] (Origin) -- (YAxisMax);% Draw y axis

    \draw [thick, darkgray!40, -latex] (Xlow) -- (XMax);% Draw x axis
    \draw [thick, darkgray!40, -latex] (Ylow) -- (YMax);% Draw y axis

    \foreach \x in {1,2,...,10}{% Two indices running over each
      \foreach \y in {1,2,...,8}{% node on the grid we have drawn 
        \node[thick,draw,circle,inner sep=2pt,fill=gray!60, name=circle-\x-\y] at (0.5*\x,0.5*\y) {};
            % Places a dot at those points
      }
    }


    \begin{pgfonlayer}{background}
    \node (B) [draw, color=BrickRed, fill=red!30, rectangle, rounded corners, fit={(circle-1-1) (circle-10-1)}] {};
    \end{pgfonlayer}

    \begin{pgfonlayer}{background}
    \node (A) [draw, color=BrickRed, fill=red!30, rectangle, rounded corners, fit={(circle-1-5) (circle-10-8)}] {};
    \end{pgfonlayer}

    \node[align=center, below] at (2.75,0) {$i$};
    \node[align=center, left]  at (0,2.25) {$j$};
    \node[align=center, left]  at (-0.5,2.25) {Data\\domain};
    \node [above] at (A.north) {$k = N -1$};
    \coordinate (bb1) at (circle-10-1);
    \node [above right of = 5pt] at (bb1) {$BB1$};
  \end{tikzpicture}
  \caption{Bounding boxes for convex union of data regions}
  \label{figure:single}
\end{figure}
\end{document}

我没有正确放置右侧内容。(例如,我尝试放置BB1,但它没有出现在正确的位置)。如何使用不同大小和样式的代码(如这里使用的)path[k][j]

这是我的输出:

在此处输入图片描述

答案1

这是一个可能的解决方案:

  • 所有节点都位于从现有圆开始的位置(但我几乎确信这个答案不会找到它们在正确的[神秘的]位置 ;-)
  • 圆圈右侧节点的字体通过样式以电传打字机方式设置
    \tikzset{我的 tt/.style={
      字体=\ttfamily#1},
    }

我们可以说可定制,使用参数(在代码中将用于设置字体的大小)。

代码:

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
%\usepackage[active,tightpage,floats]{preview}
%\setlength\PreviewBorder{30pt}%
\usetikzlibrary{calc, fit, shapes.geometric}
\pgfdeclarelayer{background}
\pgfdeclarelayer{foreground}
\pgfsetlayers{background,main,foreground}


\tikzset{my tt/.style={
  font=\ttfamily#1},
}

%\colorlet{MAROON}{Maroon}

\begin{document}

\begin{figure}[ht]
  \centering
  \begin{tikzpicture}
    \coordinate (Origin)   at (0,0);

    \coordinate (XAxisMax) at (5.5,0);
    \coordinate (YAxisMax) at (0,4.5);

    \coordinate (Ylow) at (-0.5,0);
    \coordinate (Xlow) at (0,-0.5);
    \coordinate (XMax) at (5.5,-0.5);
    \coordinate (YMax) at (-0.5,4.5);

    \draw [thick, darkgray] (Origin) -- (XAxisMax);% Draw x axis
    \draw [thick, darkgray] (Origin) -- (YAxisMax);% Draw y axis

    \draw [thick, darkgray!40, -latex] (Xlow) -- (XMax);% Draw x axis
    \draw [thick, darkgray!40, -latex] (Ylow) -- (YMax);% Draw y axis

    \foreach \x in {1,2,...,10}{% Two indices running over each
      \foreach \y in {1,2,...,8}{% node on the grid we have drawn 
        \node[thick,draw,circle,inner sep=2pt,fill=gray!60, name=\x-\y] at (0.5*\x,0.5*\y) {};
            % Places a dot at those points
      }
    }


    \begin{pgfonlayer}{background}
    \node (B) [draw, color=BrickRed, fill=red!30, rectangle, rounded corners, fit={(1-1) (10-1)}] {};
    \end{pgfonlayer}

    \begin{pgfonlayer}{background}
    \node (A) [draw, color=BrickRed, fill=red!30, rectangle, rounded corners, fit={(1-5) (10-8)}] {};
    \end{pgfonlayer}

    \node[align=center, below] at (2.75,0) {$i$};
    \node[align=center, left]  at (0,2.25) {$j$};
    \node[align=center, left]  at (-0.5,2.25) {Data\\domain};
    \node[above] at (A.north) {$k = N -1$};

    \node[right=5pt,my tt=\large] at (10-2) {BB1};
    \node[right=5pt,my tt=\scriptsize] at (10-1) {path[k][j]};

    \node[right=5pt,my tt=\scriptsize, text width=1cm] at (10-7) {path[i][j] and path[i][k]};
    \node[right=5pt,my tt=\large, anchor=south west] at (10-8) {BB0};
  \end{tikzpicture}
  \caption{Bounding boxes for convex union of data regions}
  \label{figure:single}
\end{figure}
\end{document}

结果:

在此处输入图片描述

请注意,为了更精确地定位节点,您可以使用它们的相对位置(与库positioning)以及节点的锚点。

这是一个简单的例子:

\documentclass[tikz,border=2pt,png]{standalone}
\usetikzlibrary{positioning,shapes.misc}
\pgfdeclarelayer{back}
\pgfsetlayers{back,main}
\begin{document}
\begin{tikzpicture}
\node[draw] at (0,0) (x) {Start};
\begin{pgfonlayer}{back}
 \node[draw,cross out, blue] at (x){};
\end{pgfonlayer}

% Simple usage of <position> of=<name>
\node[right of =x] {x};
\node[right of =x, anchor=north,red] {n};
\node[right of =x, anchor=north west,red!20] {nw};
\node[right of =x, anchor=north east,red!70] {ne};
\node[right of =x, anchor=south,blue] {s};
\node[right of =x, anchor=south west,blue!20] {sw};
\node[right of =x, anchor=south east,blue!70] {se};

% Usage of <position> = <distance> of <name>
\node[left=1.5cm of x, anchor=center] {x};
\node[left=1.5cm of x, anchor=north,red] {n};
\node[left=1.5cm of x, anchor=north west,red!20] {nw};
\node[left=1.5cm of x, anchor=north east,red!70] {ne};
\node[left=1.5cm of x, anchor=south,blue] {s};
\node[left=1.5cm of x, anchor=south west,blue!20] {sw};
\node[left=1.5cm of x, anchor=south east,blue!70] {se};
\end{tikzpicture}
\end{document}

结果:

在此处输入图片描述

请参阅 pgfmanual 以获取完整的锚点列表。

相关内容