3

I'm sure this has been asked here, but I can't seem to find a simple solution. I would like to use the environment to place a tikzfigure on the side of my text, as follows:

\documentclass{article}

\usepackage{lipsum}
\usepackage{wrapfig}
\usepackage{tikz}

\begin{document}

\lipsum[1]
\begin{wrapfigure}{r}{0pt}%
    \vspace{-1cm}% Doesn't do anything
    \begin{tikzpicture}%
    [every node/.style={inner sep=0,outer sep=0}]%There are no nodes, so doesn't do anything
        \foreach \z in {0,6,...,36}%
            \shade[ball color=blue] (0,\z pt) circle (3pt);%
    \end{tikzpicture}
\end{wrapfigure}
\lipsum[2-3]

\end{document}

which gives

Zoom in of spacing around blue spheres and text

The problem is that there is (IMHO) too much spacing above and below the text. When using inludegraphics I can add vspace to nudge it into place. However with tikzpicture, this doesn't work.

I found multiple questions (e.g. 1, 2) that deal with spacing around objects defined with \newcommand. Others dealt with spacing around nodes, which is not relevant here (and doesn't help anyway).

Is there a simple way to control the padding around a tikzpicture? (Closing as duplicate would be welcome)

1
  • Hello! This has nothing to do with TikZ, it's a property (hardcoded, as far as I remember) of wrapfig. You can verify this by replacing your tikzpicture with \rule{5mm}{2cm}. Also, you can use \vspace, you just have to add a star: \vspace*{-1cm}. However, it's not quite a good solution.
    – yo'
    Sep 24, 2015 at 11:17

1 Answer 1

2

Instead of using a width of 0pt, use a reasonable value like 5/6pt. And give a suitable value for the number of lines the picture will span ([4] here)

\documentclass{article}

\usepackage{lipsum}
\usepackage{wrapfig}
\usepackage{tikz}

\begin{document}

\lipsum[1]

\begin{wrapfigure}[4]{r}{7pt}%
    \vspace*{-4mm}% This works now
    \begin{tikzpicture}%
    [every node/.style={inner sep=0,outer sep=0}]%There are no nodes, so doesn't do anything
        \foreach \z in {0,6,...,36}%
            \shade[ball color=blue] (0,\z pt) circle (3pt);%
    \end{tikzpicture}%
\end{wrapfigure}
\lipsum[2]


Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
\begin{wrapfigure}[4]{r}{7pt}%
    \vspace*{-4mm}% This works now
    \begin{tikzpicture}%
    [every node/.style={inner sep=0,outer sep=0}]%There are no nodes, so doesn't do anything
        \foreach \z in {0,6,...,36}%
            \shade[ball color=blue] (0,\z pt) circle (3pt);%
    \end{tikzpicture}%
\end{wrapfigure}
Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies
et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet
magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis.
Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis
natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque
cursus luctus mauris.

\end{document}

enter image description here

You can put the picture in a box and use the width of that box like below:

\documentclass{article}

\usepackage{lipsum}
\usepackage{wrapfig}
\usepackage{tikz}

\newsavebox{\mypicture}
\sbox{\mypicture}{%
    \begin{tikzpicture}%
    [every node/.style={inner sep=0,outer sep=0}]%There are no nodes, so doesn't do anything
        \foreach \z in {0,6,...,36}%
            \shade[ball color=blue] (0,\z pt) circle (3pt);%
    \end{tikzpicture}%
    }

\begin{document}

\lipsum[1]

\begin{wrapfigure}[4]{r}{\wd\mypicture}%
    \vspace*{-4mm}% This works now
    \usebox{\mypicture}
\end{wrapfigure}
\lipsum[2]


Nam dui ligula, fringilla a, euismod sodales, sollicitudin vel, wisi.
\begin{wrapfigure}[4]{r}{\wd\mypicture}%
    \vspace*{-4mm}% This works now
    \usebox{\mypicture}
\end{wrapfigure}
Morbi
auctor lorem non justo. Nam lacus libero, pretium at, lobortis vitae, ultricies
et, tellus. Donec aliquet, tortor sed accumsan bibendum, erat ligula aliquet
magna, vitae ornare odio metus a mi. Morbi ac orci et nisl hendrerit mollis.
Suspendisse ut massa. Cras nec ante. Pellentesque a nulla. Cum sociis
natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.
Aliquam tincidunt urna. Nulla ullamcorper vestibulum turpis. Pellentesque
cursus luctus mauris.

\end{document}
5
  • So every time I change the picture I have to readjust the width... Sep 24, 2015 at 11:20
  • @AviGinsburg: Yes. Think of different images with different width. That will trouble TeX in accomodating them.
    – user11232
    Sep 24, 2015 at 11:21
  • Usually I control the width with {0.x\textwidth} or similar, causing the image to scale. This doesn't work with tikzpictures. Is there a way to pass the width of the tikzpicture to the wrapfigure? Sep 24, 2015 at 11:22
  • @AviGinsburg: Please see the edit. Is that what you want?
    – user11232
    Sep 24, 2015 at 12:38
  • Yes, but... feels so hacky. Each figure now has its own box. Well, I guess that's what I asked for. Sep 24, 2015 at 12:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .