1

Problem Description

I’d like to automatically move the tikzpicture to new page if it does not fit the current page. See the MWE—if you compile it as it is, the tikzpicture starts on the current page, but bleeds out the page edge. When this should happen, the whole tikzpicture must be automatically moved to the next page (as if you uncomment the \newpage command`.

I use xelatex.

Minimal Working Example

\documentclass[10pt, chapterprefix=false, twoside]{scrbook}

\usepackage[a4paper, top=1in, bottom=1in, right=1in, left=1in, portrait]{geometry}
\usepackage{tikz, multicol, lipsum}

\newcommand{\colbreak}{\vfill\null\columnbreak}%

\begin{document}
    \lipsum
    % \newpage
    \setlength{\columnsep}{-2.5cm}
    \begin{multicols}{2}
        \begin{tikzpicture}
            \draw[red, line width=1.6mm] (0,-9) node[below=10] {{Ω}} -- (0,0) -- (1,0) node[above=10] {{0}} node[below=10] {{0}} -- (3,0) -- (0,0) -- (0,3) node[above=10] {{Α}} -- (0,0) -- (-1,0) node[above=10] {{2}} node[below=10] {{2}} -- (-3,0);
        \end{tikzpicture}

        \colbreak

        \lipsum[1]
    \end{multicols}
\end{document}
  • Here is something that allows you to split the tikzpicture. – marmot Apr 27 at 16:32
  • why you not enclose to float environment? – Zarko Apr 27 at 17:15
  • @Zarko, I’m a *Tex newbie and I have never used a float environment. Anyway, I tried \begin{figure} .. \end{figure}, however, I does not work in multicols env (Package multicol Warning: Floats and marginpars not allowed inside `multicols' environment!), therefore I tried to put the whole multicol env into figure env. Though that moved the tikzpicture to the following page, however, it is displayed alone on that page and the next that should be after the tikzpicture, is moved before it. – tukusejssirs Apr 28 at 9:26
  • @marmot, I don’t want to split the tikzpicture, I want it to be always displayed in whole, therefore when I occurs on the bottom of a page and should it not fit in the remaining space, it should be automatically moved to the following page. Also note that the tikzpicture has some text in the right column, which also must be always on the right side of the tikzpicture. – tukusejssirs Apr 28 at 9:31
  • why you use multicol? for two column text you can use option two column in your document class, which allow to use floats. – Zarko Apr 28 at 10:17
2

One way to achieve this is to put the tikzpicture in a \savebox (such that we know its height), measure the remaining space on the page, compare it to the height of the tikzpicture and issue a \newpage if it does not fit.

\documentclass[10pt, chapterprefix=false, twoside]{scrbook}

\usepackage[a4paper, top=1in, bottom=1in, right=1in, left=1in, portrait]{geometry}
\usepackage{tikz, multicol, lipsum}

\newcommand{\colbreak}{\vfill\null\columnbreak}%
% from https://tex.stackexchange.com/a/17813/121799
\newcommand\measurepage{\dimexpr\pagegoal-\pagetotal-\baselineskip\relax}
\newsavebox{\TikZPic}
\begin{document}

    \lipsum

    \begin{lrbox}{\TikZPic}
    \begin{tikzpicture}
     \draw[red, line width=1.6mm] (0,-9) node[below=10] {{AAA}} -- (0,0) -- (1,0) node[above=10] {{0}} node[below=10] {{0}} -- (3,0) -- (0,0) -- (0,3) node[above=10] {{AAA}} -- (0,0) -- (-1,0) node[above=10] {{2}} node[below=10] {{2}} -- (-3,0);
    \end{tikzpicture}%
    \end{lrbox}
    \setlength{\columnsep}{-2.5cm}
    \ifdim\ht\TikZPic>\measurepage
    \newpage
    \fi
    \begin{multicols}{2}
    \usebox\TikZPic

        \colbreak

        \lipsum[1]
    \end{multicols}
\end{document}
  • This works as expected, though I think it would be better not to use the remaining space calculation, as it slows down the compilation, which is not a problem with a two-page document, however, I think it might be a problem when one wants to compile a book. – tukusejssirs Apr 28 at 9:41

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.