0

I haven't found any question on this but I'm having problems when placing a Tikz picture in a document. It's enclosed in a Figure environment, and it uses \centerfloat to center it, with some code in the preamble since it's not a Memoir, but an Article class.

In my MWE I added the picture between two Lorem Ipsums, but when typesetting, it appears before both of them. What is happening and how can I solve it?

\documentclass{article}
\usepackage{rotating}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,calc}
\usepackage{caption}

\makeatletter
\newcommand*{\centerfloat}{%
  \parindent \z@
  \leftskip \z@ \@plus 1fil \@minus \textwidth
  \rightskip\leftskip
  \parfillskip \z@skip}
\makeatother

\tikzset{
    every node/.style={align=center, fill=white, draw=black, thick, rectangle, outer sep=0mm, inner sep=0mm},
    rect/.style={minimum height=8mm},
    recv/.style={text width=2.5cm, rotate=90},
}

\begin{document}

    FIRST Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet ante id augue faucibus maximus sit amet vel lacus. Suspendisse potenti. Nullam metus nunc, auctor vel rutrum sed, sollicitudin vitae ante. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In ornare elit dui, sed aliquet erat pharetra ac. Nullam viverra ipsum tincidunt est cursus, at elementum neque congue. Aliquam tempor lorem ac eros mollis accumsan. Quisque vulputate consectetur ex at mattis. Ut aliquam, ligula at placerat elementum, lorem mauris convallis ante, vel faucibus nulla leo vel odio. Quisque nec ullamcorper ipsum.\\

\begin{figure}
    \centerfloat
\begin{tikzpicture}
    \node[rect, text width=4cm] (prop) at (0,0) {proposition};
    \node[rect, text width=12cm, left=0mm of prop, anchor=east] (ent) {entity};

\end{tikzpicture}
\caption{A caption}
\end{figure}

SECOND Nullam et efficitur justo. Donec sodales velit sit amet lorem varius tempus. Nulla vitae leo justo. Phasellus rhoncus egestas dui, in rutrum nisi ullamcorper sit amet. Aliquam eget imperdiet dolor. Morbi quis hendrerit erat. Nulla ornare purus ac sollicitudin dapibus.

\end{document} 

And here's the result:

enter image description here

Alenanno
  • 37,338
  • figure is a float environment, and floats have a tendency to float away. http://www.tex.ac.uk/cgi-bin/texfaq2html?label=floats – Ulrike Fischer Apr 09 '15 at 08:30
  • why define \centerfloat rather than use the standard \centering ? The only reason for using figure is to allow the figure to move, if you do not want that, just use tikzpicture directly. – David Carlisle Apr 09 '15 at 08:40
  • @DavidCarlisle Because \centering doesn't work. And without figure, I don't know how to add a proper caption. – Alenanno Apr 09 '15 at 08:43
  • 1
    Why not just add a placement specifier, \begin{figure}[!htbp] ... for example? – AboAmmar Apr 09 '15 at 08:47
  • Oh because your figure is wider than the page. Hmm \makebox is the standard way to centre that, although it isn't clear that allowing things to over-print the physical page boundaries without any warning is necessarily a good thing. There are lots of ways of captioning things eg capt-of package. – David Carlisle Apr 09 '15 at 08:51
  • @DavidCarlisle I see, I could reduce the picture I don't need it that big... Or maybe I could do the same with a standard tabular environment? – Alenanno Apr 09 '15 at 08:53
  • Is that your real use case just two words in boxes, you could just use two framebox no need for tikz or tabular at all. – David Carlisle Apr 09 '15 at 08:57
  • This question helped, and I see @AboAmmar also mentioned it. – Alenanno Apr 09 '15 at 08:57
  • @DavidCarlisle The actual picture is a bit bigger, I cut most of the picture to make the code shorter. :) – Alenanno Apr 09 '15 at 08:57

1 Answers1

0

figure is used specifically if you want the figure to float.

To add a proper caption, use captionof.

Try this

\documentclass{article}
\usepackage{rotating}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning,calc}
\usepackage{caption}

\makeatletter
\newcommand*{\centerfloat}{%
  \parindent \z@
  \leftskip \z@ \@plus 1fil \@minus \textwidth
  \rightskip\leftskip
  \parfillskip \z@skip}
\makeatother

\tikzset{
    every node/.style={align=center, fill=white, draw=black, thick, rectangle, outer sep=0mm, inner sep=0mm},
    rect/.style={minimum height=8mm},
    recv/.style={text width=2.5cm, rotate=90},
}

\begin{document}

    FIRST Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam sit amet ante id augue faucibus maximus sit amet vel lacus. Suspendisse potenti. Nullam metus nunc, auctor vel rutrum sed, sollicitudin vitae ante. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. In ornare elit dui, sed aliquet erat pharetra ac. Nullam viverra ipsum tincidunt est cursus, at elementum neque congue. Aliquam tempor lorem ac eros mollis accumsan. Quisque vulputate consectetur ex at mattis. Ut aliquam, ligula at placerat elementum, lorem mauris convallis ante, vel faucibus nulla leo vel odio. Quisque nec ullamcorper ipsum.\\

\begin{centerfloat}
  \begin{tikzpicture}
    \node[rect, text width=4cm] (prop) at (0,0) {proposition};
    \node[rect, text width=12cm, left=0mm of prop, anchor=east] (ent) {entity};

\end{tikzpicture}}
\captionof{figure}{Sample picture}
\end{centerfloat}

SECOND Nullam et efficitur justo. Donec sodales velit sit amet lorem varius tempus. Nulla vitae leo justo. Phasellus rhoncus egestas dui, in rutrum nisi ullamcorper sit amet. Aliquam eget imperdiet dolor. Morbi quis hendrerit erat. Nulla ornare purus ac sollicitudin dapibus.

\end{document}

enter image description here

Trefex
  • 1,045