Search code examples
latexfigurefloatingtikz

Prevent expanding tikz picture to wrap text around


I am new to Tikz and can not figure out why relarivly small tikzpictures expand thier width to the entire page ones. Since I want my texts floating around such figures, it would be very usefull to know how to prevent them from expanding.

Here are code and pictures of my problem and as you can see it's not caused by to long captions, what was my first idea.

result of the code below

\section{demo}

\begin{figure}[h]
    \tdplotsetmaincoords{60}{25}
    \begin{tikzpicture}[tdplot_main_coords, scale=1]
    \coordinate (o) at (0,0,0);
    \coordinate (x) at (4,0,0);
    \coordinate (y) at (0,0,4);
    \coordinate (z) at (0,-4,0); 

    \node[above] at (x) {x};
    \node[above] at (y) {y};
    \node[above] at (z) {z};

    \draw[red, -latex] (o) -- (x);
    \draw[green, -latex] (o) -- (y);
    \draw[blue, -latex] (o) -- (z);

    \end{tikzpicture}
    \caption{far to long caption for this kind of sensless figure created just for demonstrationg tikzpicture expand their width}

\end{figure}

\begin{figure}[h]
    %[... same code as above ...]   
\end{figure}

Do you guys have any suggestions to fit the tikzpicture bounds to the contents?

Thanks.


Solution

  • This is the default behavior. You can use the wrapfigure package to wrap text around a tikz figure:

    enter image description here

    \begin{wrapfigure}{r}{0.4\textwidth}
        \tdplotsetmaincoords{60}{25}
        \begin{tikzpicture}[tdplot_main_coords, scale=1]
            ...
        \end{tikzpicture}
        \caption{far to long caption for this kind of sensless figure created just for demonstrationg tikzpicture expand their width}
    \end{wrapfigure}
    

    In \begin{wrapfigure}{r}{0.4\textwidth}, the first argument is the position (r for right), the second argument is the size of the wrapfigure (here 40% of the page width fits the tikz figure).