1

I am having trouble placing TikZ inside a multicol latex without introducing unwanted whitespaces.

main.tex:

\documentclass[10pt]{article}

% Packages \usepackage{multicol,caption} \usepackage{graphicx} \usepackage{float} \usepackage{wrapfig}

\usepackage{lipsum}

\newenvironment{Figure} {\par\medskip\noindent\minipage{\linewidth}} {\endminipage\par\medskip}

\newcommand\myfigure[1]{% \medskip\noindent\begin{minipage}{\columnwidth} \centering% #1% %figure,caption, and label go here \end{minipage}\medskip}

\usepackage{titlesec} \setcounter{secnumdepth}{5} \setcounter{tocdepth}{5}

\titleclass{\subsubsubsection}{straight}[\subsection]

\newcounter{subsubsubsection}[subsubsection] \renewcommand\thesubsubsubsection{\thesubsubsection.\arabic{subsubsubsection}} \renewcommand\theparagraph{\thesubsubsubsection.\arabic{paragraph}} % optional; useful if paragraphs are to be numbered

\titleformat{\subsubsubsection} {\normalfont\normalsize\bfseries}{\thesubsubsubsection}{1em}{} \titlespacing*{\subsubsubsection} {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\makeatletter \renewcommand\paragraph{@startsection{paragraph}{5}{\z@}% {3.25ex @plus1ex @minus.2ex}% {-1em}% {\normalfont\normalsize\bfseries}} \renewcommand\subparagraph{@startsection{subparagraph}{6}{\parindent}% {3.25ex @plus1ex @minus .2ex}% {-1em}% {\normalfont\normalsize\bfseries}} \def\toclevel@subsubsubsection{4} \def\toclevel@paragraph{5}

\def\toclevel@paragraph{6} \def\l@subsubsubsection{@dottedtocline{4}{7em}{4em}} \def\l@paragraph{@dottedtocline{5}{10em}{5em}} \def\l@subparagraph{@dottedtocline{6}{14em}{6em}} \makeatother % Uses alphabets instead of numbers for the subsubsubsection \def\thesubsubsubsection{\alph{subsubsubsection}}

\usepackage{tikz} \usetikzlibrary{shapes.geometric, arrows, arrows.meta} \usepackage{makecell} \tikzstyle{startstop} = [draw, rounded rectangle, minimum width=3cm, minimum height=1cm, text width=3cm, text centered, draw=black] \tikzstyle{object} = [draw, trapezium, trapezium left angle=70, trapezium right angle=-70, minimum width=3cm, minimum height=1cm, text centered, draw=black] \tikzstyle{process} = [draw, rectangle, minimum width=3cm, minimum height=1cm, text width=3cm, text centered, draw=black] \tikzstyle{arrow} = [thick,->,>=Triangle]

\usepackage{subfiles} \providecommand{\main}{.} \geometry{a4paper, margin = 1in}

% Document \begin{document}

\begin{multicols}{2}

\subfile{sample/sample.tex}

\bibliography{main}

\end{multicols}

\end{document}

How the sample.tex looks like.

Figure Solution:

% Preamble
\providecommand{\main}{..}
\documentclass[\main/main.tex]{subfiles}

% Packages

% Document \begin{document}

\section{Section}

\lipsum[1][1-2]

\subsection{Subsection}

\lipsum[2][2-5]

\subsubsection{Subsubsection}

\lipsum[3][1-3]

\subsubsubsection{subsubsubsection}

\lipsum[3-4][1-4]

\begin{Figure} \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\captionof{figure}{Some text describing the Figure}

\end{Figure}

\lipsum[4-6]

\begin{Figure} \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\captionof{figure}{Some text describing the Figure}

\end{Figure}

\lipsum[6-7]

\end{document}

The solution suggested here does not wrap the text as expected and leaves large chunks of white space in-between paragraphs as shown by the purple arrows. A similar method also does not work and exhibits the same white spaces. white space between paragraphs

figure[H] Method:

% Same as before

\begin{figure}[H] \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\caption{Some text describing the figure}

\end{figure}

\lipsum[4-6]

\begin{figure}[H] \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\caption{Some text describing the figure}

\end{figure}

\lipsum[6-7]

\begin{figure}[H] \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\caption{Some text describing the figure}

\end{figure}

% Same as before

I also tried this method involving the float package but have the same issues. It might work for a single figure but multiple figures break it since it is removing the figure's ability to 'float' if my understanding of how figure[H] works is correct. enter image description here

wrapfigure Method:

% Same as before

\begin{wrapfigure}{l}{\linewidth} \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\caption{Some text describing the figure}

\end{wrapfigure}

\lipsum[4-6]

\begin{wrapfigure}{l}{1.0\linewidth} \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\caption{Some text describing the figure}

\end{wrapfigure}

% Same as before

Wrapping the figure as shown in Overleaf does not work if the full width is desired. It also behaves funky on the end and starts of documents. enter image description here

The figures do not have to be exactly in the places they are called but within the same page or thereabouts is also acceptable. Any solution is greatly appreciated.

Rashiq
  • 337
  • 1
    Please provide a single code ready to copy, paste and test. Possibly without tikzit which I don't find in the official TeX Live packages – Ivan Apr 25 '21 at 16:04
  • Also remove from the code anything that does not have to do with the problem in question. Thanks! – Ivan Apr 25 '21 at 16:25
  • 2
    Following Ivan's comment, the problem seems related with placing big images in minipages inside multicols, regardless of how the images are made, so consider use some as simple as \rule{\linewidth}{.5\textheight} as example image for a minimal (single) example . BTW, I would start staying with a true twocolumn document instead of multicol, and true standard figure floats instead of fake (non-floating) figure environments. – Fran Apr 25 '21 at 16:34
  • by default two column mode uses \flushbottom so if you add non-floating large figures and specify teh baseline of the last line must be at the bottom, you force latex to stretch. Use \raggedbottom` if you want figures like that. (you could have made an example with just a few lines of text) – David Carlisle Apr 25 '21 at 16:58
  • unrelated but don't load grffile (it does nothing in current version) – David Carlisle Apr 25 '21 at 17:10
  • I removed tikzit and all other unnecessary packages. Note: The problem is not present for small documents, small figures, or when a single large figure is used. It only appears when large figures are in proximity to each other with texts in between. I apologize for the sample code being too big. I'll try to make a smaller example. The reason I had included all the packages incase if there were package conflicts the community was aware of. I've had that happen to me and it took a long time to debug. As for twocolumn I have other figures that cause issues with a strict two column document. – Rashiq Apr 25 '21 at 18:48
  • See https://tex.stackexchange.com/questions/530084/i-want-to-wrap-text-only-above-and-below-a-figure-inside-of-a-column – John Kormylo Apr 26 '21 at 02:09
  • Any news? You received two answers .. . does no one is fulfill your expectations? – Zarko Feb 23 '22 at 14:29

2 Answers2

0

I had some time to kill, so I thought I would illustrate my old solution on your anything but minimal almost working example. Since there isn't enough room to fit the second figure on the page, it gets moved to the next page.

Main file (test5):

\documentclass[10pt]{article}

% Packages \usepackage{geometry}% added \usepackage{multicol,caption} \usepackage{graphicx} \usepackage{float} \usepackage{wrapfig}

\usepackage{lipsum}

\newenvironment{Figure} {\par\medskip\noindent\minipage{\linewidth}} {\endminipage\par\medskip}

\newcommand\myfigure[1]{% \medskip\noindent\begin{minipage}{\columnwidth} \centering% #1% %figure,caption, and label go here \end{minipage}\medskip}

\usepackage{titlesec} \setcounter{secnumdepth}{5} \setcounter{tocdepth}{5}

\titleclass{\subsubsubsection}{straight}[\subsection]

\newcounter{subsubsubsection}[subsubsection] \renewcommand\thesubsubsubsection{\thesubsubsection.\arabic{subsubsubsection}} \renewcommand\theparagraph{\thesubsubsubsection.\arabic{paragraph}} % optional; useful if paragraphs are to be numbered

\titleformat{\subsubsubsection} {\normalfont\normalsize\bfseries}{\thesubsubsubsection}{1em}{} \titlespacing*{\subsubsubsection} {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\makeatletter \renewcommand\paragraph{@startsection{paragraph}{5}{\z@}% {3.25ex @plus1ex @minus.2ex}% {-1em}% {\normalfont\normalsize\bfseries}} \renewcommand\subparagraph{@startsection{subparagraph}{6}{\parindent}% {3.25ex @plus1ex @minus .2ex}% {-1em}% {\normalfont\normalsize\bfseries}} \def\toclevel@subsubsubsection{4} \def\toclevel@paragraph{5}

\def\toclevel@paragraph{6} \def\l@subsubsubsection{@dottedtocline{4}{7em}{4em}} \def\l@paragraph{@dottedtocline{5}{10em}{5em}} \def\l@subparagraph{@dottedtocline{6}{14em}{6em}} \makeatother % Uses alphabets instead of numbers for the subsubsubsection \def\thesubsubsubsection{\alph{subsubsubsection}}

\usepackage{tikz} \usetikzlibrary{shapes, arrows, arrows.meta} \usepackage{makecell} % note: \tikxstyle obsolete \tikzset{startstop/.style = {draw, rounded rectangle, minimum width=3cm, minimum height=1cm, text width=3cm, text centered, draw=black}} \tikzset{object/.style = {draw, trapezium, trapezium left angle=70, trapezium right angle=-70, minimum width=3cm, minimum height=1cm, text centered, draw=black}} \tikzset{process/.style = {draw, rectangle, minimum width=3cm, minimum height=1cm, text width=3cm, text centered, draw=black}} \tikzset{arrow/.style = {thick,->,>=Triangle}}

\usepackage{subfiles}% \providecommand{\main}{.} \geometry{a4paper, margin = 1in} %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% \makeatletter \newcommand{\split}[2]% #1 = text, #2 = insert {\par\bgroup \setbox0=\vbox{\strut #1\strut}% measure height \sbox1{#2}% ditto \dimen0=\dimexpr \pagegoal-\pagetotal-\col@number\baselineskip\relax% fudge factor \dimen1=\pagegoal% compute height of one column \ifnum\col@number>1\relax \divide\dimen1 by \col@number \fi \advance\dimen1 by -\baselineskip% fudge factor \loop\ifdim\dimen0>\dimen1% space left in this column \advance\dimen0 by -\dimen1 \repeat \dimen1=\dimexpr \ht1+\dp1+\textfloatsep\relax% space needed for insert \ifdim\dimen1>\dimen0 \ifdim\ht0>\dimen0 \setbox2=\vsplit0 to \dimen0 \unvbox2 \noindent\box1 \vskip\textfloatsep \unvbox0 \else% entire text will fit into column \unvbox0 \vskip\textfloatsep% probably ignored \noindent\box1 \vskip\textfloatsep \fi \else% insert will fit into column \vskip\intextsep \noindent\box1 \vskip\intextsep \unvbox0 \fi \egroup\par} \makeatother %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Document \begin{document}

\begin{multicols}{2}

\subfile{test6}% filename changed

%\bibliography{main}% no bib file

\end{multicols}

\end{document}

Subfile (test6):

% Preamble
\providecommand{\main}{..}
\documentclass[test5]{subfiles}% finlename change

% Packages

% Document \begin{document}

\section{Section}

\lipsum[1][1-2]

\subsection{Subsection}

\lipsum[2][2-5]

\subsubsection{Subsubsection}

\lipsum[3][1-3]

\subsubsubsection{subsubsubsection}

\lipsum[3-4][1-4]

\split{\lipsum[4-6]}{% \begin{Figure} \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\captionof{figure}{Some text describing the Figure}

\end{Figure}}

\split{\lipsum[6-7]}{% \begin{Figure} \centering \begin{tikzpicture}[node distance=2cm] \node(start)[startstop]{Start}; \node(nd1)[object, below of=start]{Node1}; \node(nd2)[process, below of=nd1]{\makecell[c]{Node \ 2}}; \node(nd3)[process, below of=nd2]{Node3}; \node(nd4)[object, below of=nd3]{\makecell[c]{Node4}}; \node(end)[startstop, below of=nd4]{End};

  \draw[arrow](start)--(nd1);
  \draw[arrow](nd1)--(nd2);
  \draw[arrow](nd2)--(nd3);
  \draw[arrow](nd3)--(nd4);
  \draw[arrow](nd4)--(end);
\end{tikzpicture}
\captionof{figure}{Some text describing the Figure}

\end{Figure}}

\end{document}

John Kormylo
  • 79,712
  • 3
  • 50
  • 120
0

It seems that images are to big that both can be placed on the same page immediately after their points of insertion in to the text. If you allowed figures to float, than the second image will follow the first as close as possible on the next page, i.e. on the top of the next page.

If this is allowed (tolerated?), hen the following result is easy to achieve:

enter image description here

\documentclass[twocolumn]{article}
% Packages
\usepackage[a4paper, margin = 1in]{geometry}
\usepackage{caption}
\usepackage{graphicx}

\usepackage{titlesec} \setcounter{secnumdepth}{5} \setcounter{tocdepth}{5}

\titleclass{\subsubsubsection}{straight}[\subsection]

\newcounter{subsubsubsection}[subsubsection] \renewcommand\thesubsubsubsection{\thesubsubsection.\arabic{subsubsubsection}} \renewcommand\theparagraph{\thesubsubsubsection.\arabic{paragraph}} % optional; useful if paragraphs are to be numbered

\titleformat{\subsubsubsection} {\normalfont\normalsize\bfseries}{\thesubsubsubsection}{1em}{} \titlespacing*{\subsubsubsection} {0pt}{3.25ex plus 1ex minus .2ex}{1.5ex plus .2ex}

\makeatletter \renewcommand\paragraph{@startsection{paragraph}{5}{\z@}% {3.25ex @plus1ex @minus.2ex}% {-1em}% {\normalfont\normalsize\bfseries}} \renewcommand\subparagraph{@startsection{subparagraph}{6}{\parindent}% {3.25ex @plus1ex @minus .2ex}% {-1em}% {\normalfont\normalsize\bfseries}} \def\toclevel@subsubsubsection{4} \def\toclevel@paragraph{5}

\def\toclevel@paragraph{6} \def\l@subsubsubsection{@dottedtocline{4}{7em}{4em}} \def\l@paragraph{@dottedtocline{5}{10em}{5em}} \def\l@subparagraph{@dottedtocline{6}{14em}{6em}} \makeatother % Uses alphabets instead of numbers for the subsubsubsection \def\thesubsubsubsection{\alph{subsubsubsection}}

\usepackage{tikz} \usetikzlibrary{arrows.meta, chains, positioning, shapes.geometric} \tikzset{ > = Triangle, base/.style = {draw, minimum height=1cm, text width=3cm, align=center}, startstop/.style = {base, rounded corners}, object/.style = {base, trapezium, trapezium stretches body, trapezium left angle=70, trapezium right angle=110}, process/.style = {base}, arrow/.style = {thick,->} } \usepackage{lipsum}

\begin{document}

\section{Section} \lipsum[1][1-2]

\subsection{Subsection} \lipsum[2][2-5]

\subsubsection{Subsubsection} \lipsum[3][1-3]

\subsubsubsection{subsubsubsection} \lipsum[3-4][1-4] \textbf{The first image is inserted here}

\begin{figure}[htb]
\centering
\begin{tikzpicture}[

node distance = 9mm, start chain = going below ] \begin{scope}[nodes={on chain, join=by arrow}] \node [startstop] {Start}; \node [object] {Node1}; \node [process] {Node \ 2}; \node [process] {Node3}; \node [object] {Node4}; \node [startstop] {End}; \end{scope} \end{tikzpicture} \caption{Some text describing the Figure} \label{fig:1} \end{figure}

\lipsum[4-6] \textbf{The second image is inserted here}

\begin{figure}[ht]
\centering
\begin{tikzpicture}[

node distance = 9mm, start chain = going below ] \begin{scope}[nodes={on chain, join=by arrow}] \node [startstop] {Start}; \node [object] {Node1}; \node [process] {Node \ 2}; \node [process] {Node3}; \node [object] {Node4}; \node [startstop] {End}; \end{scope} \end{tikzpicture} \caption{Some text describing the Figure} \label{fig:2} \end{figure}

\lipsum[6-7]

\textbf{The rest of article}

\end{document}

If this is not acceptable, you there is not many option what can be done. Among them can be:

  • Reduce height of image, At images shown in question this can be achieved by reducing distances between nodes, or by reducing minimum height of nodes and use smaller font size.
  • Move image earlier on page, for example for one paragraph. In this case can happen, that first mention of this image is on the next page This can eventually can be fixed by rephrasing text in this paragraph. For example:

enter image description here

% preamble as before
\begin{document}

\section{Section} \lipsum[1][1-2]

\subsection{Subsection} \lipsum[2][2-5]

\subsubsection{Subsubsection} \lipsum[3][1-3]

\subsubsubsection{subsubsubsection} \lipsum[3-4][1-4] \textbf{The first image is inserted here}

\begin{figure}[htb]
\centering
\begin{tikzpicture}[

node distance = 9mm, start chain = going below ] \begin{scope}[nodes={on chain, join=by arrow}] \node [startstop] {Start}; \node [object] {Node1}; \node [process] {Node \ 2}; \node [process] {Node3}; \node [object] {Node4}; \node [startstop] {End}; \end{scope} \end{tikzpicture} \caption{Some text describing the Figure} \label{fig:1} \end{figure}

\lipsum[4-5] %<--- \textbf{The second image is inserted in text earlier}

\begin{figure}[ht]
\centering
\begin{tikzpicture}[

node distance = 9mm, start chain = going below ] \begin{scope}[nodes={on chain, join=by arrow}] \node [startstop] {Start}; \node [object] {Node1}; \node [process] {Node \ 2}; \node [process] {Node3}; \node [object] {Node4}; \node [startstop] {End}; \end{scope} \end{tikzpicture} \caption{Some text describing the Figure} \label{fig:2} \end{figure} \textbf{Rephrase paragraph, that figure 2 will mentioned earlier in paragraph, for example \emph{here}} \lipsum[5] \textbf{Originally was here}

\lipsum[6-7] \textbf{The rest of article}

\end{document}

Off-topic: I took a liberty and shorten your tikzpicture code.

Zarko
  • 296,517