I try to modify rectangle bounding box to square and plus 10% enlarge! I wish below logic work but error reported:

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

Error message:

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

l.30               \n1={max(\x2-\x1,\y3-\y1)*1.1}
                                                 ,
? 
  • I guess you're missing some \makeatletter, aren't you? – marmot Dec 7 at 2:00
  • Why two \p2 ? You can't define \x3 and \y3 (these values are automatically defined from point \p3). – Paul Gaborit Dec 7 at 2:02
  • @PaulGaborit Thanks,it's really a big problem! – lucky1928 Dec 7 at 2:37
up vote 2 down vote accepted

To increase the bounding box, you do not need any of these. Most likely there is a predefined way, but even without opening the pgfmanual you could just do

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

enter image description here

Sorry, I did not read carefully enough. Yes, for a quadratic bounding box, calc is very handy.

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

enter image description here

Why did your code throw errors? You were mixing a few things (as also observed independently by Paul Gaborit), and I fixed them to arrive at

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

enter image description here

Note that I commented out save path, as this key is already predefined. What is not predefined (unless you load the spath3 library) is the use path key, which can be found here. (I am not a historian, so I do not know who had it first.) As for your real problem, you cannot assign values to \x3, say, only to \n1, \n2 etc. \x1 and \y1 are the coordinates of \p1 and so on.

  • Great answer! Thanks, for solution 1, I prefer square but not rectangle! Not sure how to modify it as a square! – lucky1928 Dec 7 at 2:41
  • @lucky1928 Sorry for not reading carefully enough. I added a simple trick for a quadratic box. (As far as the bounding box is concerned, a circle and a square do the same.) – marmot Dec 7 at 4:01
  • Good idea, I am curious about your drawing is circle, so I think it should be a circle bounding box, but actually it's a rectange as expected! – lucky1928 Dec 7 at 15:31

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.