修改边界框为正方形

修改边界框为正方形

我尝试将矩形边界框修改为正方形并放大 10%!我希望下面的逻辑能够工作,但报告了错误:

\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usetikzlibrary{backgrounds}
\begin{document}
    \begin{tikzpicture}
    %\tikzset{background rectangle/.style={fill=gray!20}, show background rectangle}
    \tikzset{
        save path/.code 2 args={
            \pgfkeysalso{#1/.estyle={insert path={#2}}}%
            \global\expandafter\let\csname pgfk@\pgfkeyscurrentpath/.@cmd\expandafter\endcsname
            \csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname
            \pgfkeysalso{#1}%
        }
    }
    \coordinate (O) at (5,5);
    \foreach \i in {30,150,-30,-150} {
        \begin{scope}[rotate around={\i:(O)}]    
            \coordinate (P) at (7.45,5);    
            \begin{scope}[rotate around={180:(P)}]
                \draw[fill=white] (5.65,5) -- ++(1.8,1.5) -- ++(0,-1) -- ++(1.8,0) 
                -- ++(0,-1) -- ++(-1.8,0) -- ++(0,-1) -- cycle;
            \end{scope}
        \end{scope}
    }

    \path let \p1=(current bounding box.center),
              \p2=(current bounding box.east),
              \p2=(current bounding box.north),
              \n1={max(\x2-\x1,\y3-\y1)*1.1},
              \x3={x1-\n1},
              \y3={y1-\n1},
              \x4={x1+\n1},
              \y4={y1+\n1}
              in 
              useasboundingbox (\x3,\y3) rectangle (\x4,\y4);
    \end{tikzpicture}
\end{document}

错误信息:

! Undefined control sequence.
\pgfmath@dimen@ ...men@@ #1=0.0pt\relax \pgfmath@ 

l.30               \n1={max(\x2-\x1,\y3-\y1)*1.1}
                                                 ,
? 

答案1

要增加边界框,您不需要任何这些。很可能有一种预定义的方法,但即使不打开 pgfmanual,您也可以这样做

\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\begin{document}
    \begin{tikzpicture}
    \coordinate (O) at (5,5);
    \foreach \i in {30,150,-30,-150} {
        \begin{scope}[rotate around={\i:(O)}]    
            \coordinate (P) at (7.45,5);    
            \begin{scope}[rotate around={180:(P)}]
                \draw[fill=white] (5.65,5) -- ++(1.8,1.5) -- ++(0,-1) -- ++(1.8,0) 
                -- ++(0,-1) -- ++(-1.8,0) -- ++(0,-1) -- cycle;
            \end{scope}
        \end{scope}
    }

    \path (current bounding box.south west) -- (current bounding box.north east)
    coordinate[pos=-0.1] (sw) coordinate[pos=1.1] (ne);
    \path[use as bounding box] (sw) rectangle (ne);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

抱歉,我读得不够仔细。是的,对于二次边界框来说,calc非常方便。

\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
    \begin{tikzpicture}
    \coordinate (O) at (5,5);
    \foreach \i in {30,150,-30,-150} {
        \begin{scope}[rotate around={\i:(O)}]    
            \coordinate (P) at (7.45,5);    
            \begin{scope}[rotate around={180:(P)}]
                \draw[fill=white] (5.65,5) -- ++(1.8,1.5) -- ++(0,-1) -- ++(1.8,0) 
                -- ++(0,-1) -- ++(-1.8,0) -- ++(0,-1) -- cycle;
            \end{scope}
        \end{scope}
    }

    \path[use as bounding box] let \p1=(current bounding box.south west),\p2=(current bounding
    box.center) in (\p2) circle ({1.1*max(\x2-\x1,\y2-\y1)});
    \end{tikzpicture}
\end{document}

在此处输入图片描述

为什么你的代码会出错?你把一些东西混在一起了(Paul Gaborit 也独立观察到了这一点),我修复了这些问题,最终得到了

\documentclass[border=0pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,intersections}
\usetikzlibrary{backgrounds}
\begin{document}
    \begin{tikzpicture}
    %\tikzset{background rectangle/.style={fill=gray!20}, show background rectangle}
%     \tikzset{
%         save path/.code 2 args={
%             \pgfkeysalso{#1/.estyle={insert path={#2}}}%
%             \global\expandafter\let\csname pgfk@\pgfkeyscurrentpath/.@cmd\expandafter\endcsname
%             \csname pgfk@\pgfkeyscurrentpath/.@cmd\endcsname
%             \pgfkeysalso{#1}%
%         }
%     }
    \coordinate (O) at (5,5);
    \foreach \i in {30,150,-30,-150} {
        \begin{scope}[rotate around={\i:(O)}]    
            \coordinate (P) at (7.45,5);    
            \begin{scope}[rotate around={180:(P)}]
                \draw[fill=white] (5.65,5) -- ++(1.8,1.5) -- ++(0,-1) -- ++(1.8,0) 
                -- ++(0,-1) -- ++(-1.8,0) -- ++(0,-1) -- cycle;
            \end{scope}
        \end{scope}
    }

    \path let \p1=(current bounding box.center),
              \p2=(current bounding box.east),
              \p3=(current bounding box.north),
              \n1={max(\x2-\x1,\y3-\y1)*1.1},
              \n2={\x1-\n1},
              \n3={\y1-\n1},
              \n4={\x1+\n1},
              \n5={\y1+\n1}
              in 
              [use as bounding box] (\n2,\n3) rectangle (\n4,\n5);
    \end{tikzpicture}
\end{document}

在此处输入图片描述

请注意,我注释掉了save path,因为这个键已经预定义了。未预定义的(除非你加载库spath3)是use path键,可以在这里。(我不是历史学家,所以我不知道谁先有了它。)至于你的真正问题,你不能为 分配值,\x3比如说,只能为 等分配值。并且是 和 的坐标\n1\n2等等。\x1\y1\p1

相关内容