0

I am trying to make TikZ diagrams such that there is always 1cm of vertical space before the drawing. Is this possible?

This code does not compile.

\documentclass{article}

\usepackage{tikz}

\renewcommand{\begin{tikzpicture}}
{\vspace{1cm} \begin{tikzpicture}}

\begin{document}

text before
\begin{tikzpicture}
\draw(-3,3)--(2,1);
\end{tikzpicture}
text after

\end{document}
4
  • may be you mean \renewenvironment there and sure not \renewcommand ... – koleygr Sep 2 '17 at 19:15
  • I just tried changing it to \renewenvironment but it still does not work. – WeCanLearnAnything Sep 2 '17 at 19:20
  • You shouldn't do this. Redefining tikzpicture in this way can only give grief. Define a new environment that you use either instead or around tikzpicture. – Ulrike Fischer Sep 2 '17 at 19:21
  • Yes... but this is the way to change an environment. You can not do it with renewcommand. Just trying to find a way (Many environments refuse to renew) – koleygr Sep 2 '17 at 19:23
1

As @UlrikeFischer advices, is easier to define your tikzpicture:

\documentclass{article}

\usepackage{tikz}

\newenvironment{mytikzpicture}{\begin{center}\begin{tikzpicture}}
{\
\end{tikzpicture}\end{center}\vspace*{1cm}

}

\begin{document}

text before

\begin{mytikzpicture}
   \draw(-3,3)--(2,1);
\end{mytikzpicture}
text after
\end{document}

Output:

enter image description here

For them who really want the space before (and not after) tikzpicture the code is:

\documentclass{article}

\usepackage{tikz}

\newenvironment{mytikzpicture}{\vspace*{1cm}

\begin{center}\begin{tikzpicture}}
{\
\end{tikzpicture}\end{center}
}

\begin{document}

text before
\begin{mytikzpicture}
   \draw(-3,3)rectangle(2,1);
\end{mytikzpicture}
text after
\end{document}
2
  • Awesome, thanks! I had completely forgot about \renewcommand as I hadn't used it in forever. – WeCanLearnAnything Sep 2 '17 at 19:39
  • Welcome. \renewenvironment is also a good command but many environments are refusing to renew... and in these cases @UlrikeFischer's advice is really helpful. – koleygr Sep 2 '17 at 19:42
0

You could also change the current bounding box just before the tikzpicture is closed with execute at end picture option. This option can be applied or not on tikzpicture parameters.

Following example enlarges tikzpictures to north with 1cm space. show background rectangle provides visibility to the change.

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{backgrounds}

\tikzset{%
    add1cm/.style={%
        execute at end picture={\path (current bounding box.north)--++(0,1cm);
        }
    },
    background rectangle/.style={fill=blue!30}
}

\begin{document}

\begin{tikzpicture}[add1cm, show background rectangle]
  \draw (0,0) rectangle (3,2);
\end{tikzpicture}

\begin{tikzpicture}[show background rectangle]
  \draw (0,0) rectangle (3,2);
\end{tikzpicture}
\end{document}

enter image description here

3
  • How can I change this example to add space at the bottom (e.g. south instead of north? I tried to replace box.north with box.south but that does not yield any effect. – lanoxx Aug 26 '18 at 14:26
  • @lanoxx did you also change (0,1) by (0,-1)? – Ignasi Aug 26 '18 at 16:37
  • Ah, it did not occur to me that I had to use a negative value, using a negative value works. Thanks a lot. – lanoxx Aug 26 '18 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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