如何在 PGF/Tikz 中准备类似 PSTricks 的画布(或绘图区域)并在其中绘制其他对象之前?

如何在 PGF/Tikz 中准备类似 PSTricks 的画布(或绘图区域)并在其中绘制其他对象之前?

目标:

  1. 提前显示指定大小的画布边界框。换句话说,我想先创建一个\begin{pspicture}(-2,-2)(3,3)类似画布,然后再在其中绘制其他对象。出于某些原因,我不喜欢 Tikz 生成的自动确定画布大小。

  2. 显示带有数字的彩色网格,用于导航。它还会剪切超出其边界的任何对象。网格可以以两种方式应用于画布:永久和临时。

  3. 隐藏全球临时电网,而不必逐一进行调整\tikzpicture(这是繁琐的)。

如果我提供 PSTricks 中的示例以使其更清楚,那就更好了。


技巧

替代文本

\documentclass[dvipsnames,dvips,rgb]{minimal}
\usepackage{pstricks}

\newpsstyle{gridstyle}{%
gridwidth=0.4pt,%default: 0.8pt
gridcolor=Red!10,%default: black
griddots=0,%default: 0 
%
gridlabels=3pt,%default: 10pt
gridlabelcolor=Blue,%default: black
%
subgriddiv=5,%default: 5
subgridwidth=0.2pt,%default: 0.4pt
subgridcolor=Green!10,%default: gray
subgriddots=0%default: 0
}

\psset{style=gridstyle}



%To remove the grid globally, please remove th following comment. 
%\let\psgrid\relax

\begin{document}

\begin{pspicture}[showgrid](-2,-2)(3,3)
    %Permanent grid has been setup.
    %other objects start from here
    \pscircle[fillstyle=solid,opacity=0.25,fillcolor=red](0.5,0.5){2}  
\end{pspicture} 

\vspace{1cm}

\begin{pspicture}(-2,-2)(3,3)
    \psgrid%this is a temporary grid that can be hidden globally later.
    %other objects start from here
    \pscircle[fillstyle=solid,opacity=0.25,fillcolor=red](0.5,0.5){2}  
\end{pspicture} 
\end{document}

PGF/Tikz

我下面的尝试并没有产生与上面的 PSTricks 相同的数字。

替代文本

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\clip (-2,-2) rectangle (3,3);
\draw (-2,-2) grid (3,3);
\draw[opacity=0.25,fill=red] (0.5,0.5) circle (2); 
\end{tikzpicture}
\end{document}

根据@Torbjorn 的建议首次尝试

它比上面给出的 PSTricks 更好,但不相似。

\documentclass{minimal}
\usepackage{tikz}
\begin{document}

\newcommand{\grid}[4]{\draw [green] (#1,#2) grid (#3,#4);}

\begin{tikzpicture}
\clip (-2,-2) rectangle (3,3);
\draw [opacity=0.25,fill=red,draw=black] (0.5,0.5) circle (2); 
\draw [green] (-2,-2) grid (3,3);%Permanent
\end{tikzpicture}

\vspace{1cm}

\begin{tikzpicture}
\clip (-2,-2) rectangle (3,3);
\draw [opacity=0.25,fill=red,draw=black] (0.5,0.5) circle (2); 
%
%I hope it to be temporarily grid that can be removed globally later.
\grid{-2}{-2}{3}{3}
\end{tikzpicture}
\end{document}

答案1

要将图片限制在特定的矩形内,请\clip在图片开头添加命令,例如

\clip (-2,-2) rectangle (3,3);

这适用于大多数路径(但grid似乎不是),因此您可以根据需要手动指定顶点或使用圆形而不是矩形。

因为网格是为了导航目的,所以我认为最好把它放在最上面。你可以把它放在图片的末尾,或者使用键execute at end picture,如下所示:

\documentclass{minimal}
\usepackage[demo]{graphicx}
\usepackage{tikz}
\begin{document}
\noindent
\begin{tikzpicture}[%
execute at begin picture={\clip (0,0) rectangle (5,5);},
execute at end picture={\draw [red] (0,0) grid (5,5);}]
\draw (2.5,2.5) node[inner sep=0]{\includegraphics[width=5cm]{}};
\end{tikzpicture}
\end{document}

要删除网格,只需注释掉绘制网格的行即可。


(该帖子的其余部分可能不太相关,但我会保留原样。)


PGF 手册描述了两种定义边界框的方法tikzpicture(请参阅 PGF 2.10 手册第 15.7 节)。要么使用命令\useasboundingbox,要么将其用作use as bounding box路径的选项。请注意,这不会不是剪切路径,因此如果您在指定的边界框之外绘制某些内容,它将流入周围的文本和/或边距。(要同时剪切图片,请使用命令\clip。)

通过使用后一个选项,您可以通过将选项添加到命令中同时绘制边界框的边框\draw。例如:

\documentclass{minimal}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\begin{tikzpicture}
  \draw[blue, use as bounding box] (0,0) rectangle (2,2);
  \draw (-2,-2) -- (4,4);  % these demonstrate that 
  \draw (-2,4) -- (4,-2);  % paths are not clipped
\end{tikzpicture}
\lipsum[2]
\end{document}

显示上述代码输出的图像

如果你只想设置边界框,而不是绘制边框,你可以使用

\useasboundingbox (0,0) rectangle (2,2);

绘制图片边框的另一种方法是使用预定义节点current bounding box。这是一个矩形形状,其大小始终与当前边界框相同。您可以使用它的角作为坐标来绘制边框。此外,通过使用键,execute at end picture您可以将绘图命令作为开启选项添加到环境中tikzpicture。(当然,您也可以将其添加到图片末尾。)

\documentclass{minimal}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[execute at end picture={%
\draw [blue] (current bounding box.south west) rectangle 
    (current bounding box.north east);}]
  \draw (0,0) circle (1cm);
  \draw (3,0) circle (3pt);
  \end{tikzpicture}
\end{document}

显示上述代码输出的图像


关于您的示例,水平“边距”与垂直“边距”不同的原因似乎是inner sep包含图像的节点的。正是这个节点决定了的宽度tikzpicture,而网格决定了高度。如果移除网格,您会看到边界框到图像的距离在两个方向上都是相同的。

内部分隔的尺寸可以通过inner sep=<length>在节点中添加选项来设置。也可以分别使用inner xsep=<length>和来为水平和垂直方向单独定义inner ysep=<length>

\documentclass{minimal}
\usepackage[demo]{graphicx}
\usepackage{tikz}
\begin{document}
\noindent%
\begin{tikzpicture}[execute at end picture=%
{\draw [ultra thick] (current bounding box.south west) rectangle
(current bounding box.north east);}]
\draw (2.5,2.5) node[inner sep=0pt]{\includegraphics[width=5cm]{}};
\draw[draw=red] (0,0) grid (5,5);
\end{tikzpicture}
\end{document}

答案2

编辑:相同的想法,但仅使用 Ti样式

这些样式可以 (1) 固定边界框,(2) 绘制网格和子网格,(3) 绘制标签,以及 (4) 固定剪裁框。注意:边界框的标签会被忽略。

以下是序言和一些有用的样式:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\tikzset{
  % fix bounding box <= two points
  fix bounding box/.style 2 args={execute at begin scope={
      \path[use as bounding box] (#1) rectangle (#2);
    },
  },
  % fix clipping box <= two points
  fix clipping box/.style 2 args={execute at begin scope={
      \clip (#1) rectangle (#2);
    },
  },
  % draw styled grid <= style and two points
  draw styled grid/.style n args={3}{execute at begin scope={
      \path
      let \p1=(#2), \p2=(#3) in
      \pgfextra{ % fixed order for grid !!!
        \pgfmathsetmacro{\xmin}{min(\x1,\x2)}
        \pgfmathsetmacro{\xmax}{max(\x1,\x2)}
        \pgfmathsetmacro{\ymin}{min(\y1,\y2)}
        \pgfmathsetmacro{\ymax}{max(\y1,\y2)}
      }
      [#1] (\xmin pt,\ymin pt) grid (\xmax pt,\ymax pt);
    },
  },
  % draw grid labels <= two points
  % (first point is used to position labels)
  draw grid labels/.style 2 args={execute at begin scope={
      \path
      let \p1=(#1), \p2=(#2) in
      \pgfextra{
        \pgfmathsetmacro{\xmin}{min(\x1,\x2)}
        \pgfmathsetmacro{\xmax}{max(\x1,\x2)}
        \pgfmathsetmacro{\ymin}{min(\y1,\y2)}
        \pgfmathsetmacro{\ymax}{max(\y1,\y2)}
        \pgfmathsetmacro{\xmincm}{\xmin*0.036}
        \pgfmathsetmacro{\xmaxcm}{\xmax*0.036}
        \pgfmathsetmacro{\ymincm}{\ymin*0.036}
        \pgfmathsetmacro{\ymaxcm}{\ymax*0.036}
        \pgfmathtruncatemacro{\xbeg}{ceil(\xmincm)}
        \pgfmathtruncatemacro{\xend}{floor(\xmaxcm)}
        \pgfmathtruncatemacro{\ybeg}{ceil(\ymincm)}
        \pgfmathtruncatemacro{\yend}{floor(\ymaxcm)}
      }
      \foreach \x in {\xbeg,...,\xend} {
        node[x labelsstyle,at={(\x, 0 |- #1)}]{\x}
      }
      \foreach \y in {\ybeg,...,\yend} {
        node[y labelsstyle,at={(#1 |- 0, \y)}]{\y}
      };
    }
  },
  % fix grids labels and clip <= two points
  fix grids labels and clip/.style 2 args={
    fix bounding box={#1}{#2},
    draw styled grid={subgridstyle}{#1}{#2},
    draw styled grid={gridstyle}{#1}{#2},
    draw grid labels={#1}{#2},
    fix clipping box={#1}{#2},
  },
  % fix and clip <= two points
  fix and clip/.style 2 args={
    fix bounding box={#1}{#2},
    fix clipping box={#1}{#2},
  },
  % default styles for grid, subgrid and labels
  gridstyle/.style={step=1,line width=.8pt,draw=black},
  subgridstyle/.style={step=.2,line width=.4pt,draw=gray},
  labelsstyle/.style={font=\scriptsize,text=black},
  x labelsstyle/.style={labelsstyle,below},
  y labelsstyle/.style={labelsstyle,left},
}

代码:

\pagestyle{empty}
\usepackage{parskip}

\tikzset{
  custom grid/.style={
    gridstyle/.style={step=1,line width=.4pt,draw=red!10},
    subgridstyle/.style={step=.25,line width=.2pt,draw=green!10},
    labelsstyle/.style={font=\tiny,text=blue},
    y labelsstyle/.style={labelsstyle,right},
  }
}
\begin{document}


Default grids and labels:

\begin{tikzpicture}[fix grids labels and clip={-2,-2}{3,3}]
  \draw [fill opacity=0.25,fill=red,draw=black] (0.5,0.5) circle (2); 
  \draw [fill opacity=1,fill=blue,draw=black,even odd rule]
  (0.5,0.5) circle (1.5) circle(1); 
  \draw (3,3) grid (-2,-2);
  \draw [fill=lime,fill opacity=.5] (2.5,-1.5) circle (1);
\end{tikzpicture}

Grids and labels with specific styles:

\begin{tikzpicture}[custom grid,fix grids labels and clip={3,-2}{-2,3}]
  \draw [fill opacity=0.25,fill=red,draw=black] (0.5,0.5) circle (2); 
  \draw [fill opacity=1,fill=blue,draw=black,even odd rule]
  (0.5,0.5) circle (1.5) circle(1); 
  \draw [fill=lime,fill opacity=.5] (2.5,-1.5) circle (1);
\end{tikzpicture}


Just a fixed boundind box with clipping (useful with a local
scope):

\begin{tikzpicture}[fix and clip={-2,-2}{3,3}]
  \draw [fill opacity=0.25,fill=red,draw=black] (0.5,0.5) circle (2); 
  \draw [fill opacity=1,fill=blue,draw=black,even odd rule]
  (0.5,0.5) circle (1.5) circle(1); 
  \draw [fill=lime,fill opacity=.5] (2.5,-1.5) circle (1);
  \begin{scope}[shift={(1,1)},rotate=20,fix grids labels and clip={-1,-1}{1,1}]
    \fill[red] (.8,0) circle (2pt);
  \end{scope}
\end{tikzpicture}

Some text...
\end{document}

结果如下:

在此处输入图片描述

答案3

tikzpicture通过放入的混合解决方案pspicture

在此处输入图片描述

\documentclass[border=12pt,pstricks]{standalone}
\usepackage{pstricks,tikz}

\begin{document}
\begin{pspicture}[showgrid=top](6,6)
\rput(3,3){%
%============ BEGIN TIKZ ============
\begin{tikzpicture}
\draw (0,0) grid (6,6);
\end{tikzpicture}%
%============ END TIKZ ============
}
\end{pspicture}
\end{document}

相关内容