3

I write a command to wrap text around tikz picture automatically (Image 1). enter image description here

I want the linewidth can be expand when the text is higher than the image height (Image 2). enter image description here

Please help correct below code.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum} 
\newdimen\widthimmini
\newdimen\heightimmini
\newdimen\textimagewidth
\newbox\imbox
\newcommand{\putimage}[3][-12pt]{
        \setbox\imbox=\vbox{\hbox{#3}}
        \widthimmini=\wd\imbox
        \heightimmini=\ht\imbox 
        \textimagewidth=\dimexpr\linewidth - 1.075\widthimmini\relax
\par\vspace*{\dimexpr#1\relax}\noindent{\ignorespaces\begin{minipage}[t]{\textimagewidth}
    \vspace*{0pt}\ignorespaces
    \begingroup
    #2
    \endgroup
    \end{minipage}
    \begin{minipage}[t]{\widthimmini}
    \vspace*{-1pt}
\begin{center}
    \begingroup
        #3
    \endgroup
\end{center}
\end{minipage}}}
\begin{document}
\putimage{ \lipsum[30]
}
{\begin{tikzpicture}
\draw (0,0) circle(2);
\end{tikzpicture}}

\newpage
\putimage{ \lipsum[10]
}
{\begin{tikzpicture}
\draw (0,0) circle(2);
\end{tikzpicture}}

\noindent
\lipsum[5]
\end{document}

1 Answer 1

8

You're making things needlessly complicated: the plain TeX insbox macro package can do what you want, and it works fine here. It defines an \InsertBoxR command which takes two mandatory arguments: the number of lines unshortened before the box is inserted, and the box inserted, and one optional argument: the number of supplementary shortened lines, in case latex makes a wrong calculation of the necessary number of shortened lines to place the box.

There is also an \InsertBoxL and an \InsertBoxC commands.

Here is a possible code:

 \documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\input{insbox.tex}

\begin{document}

\InsertBoxR{0}{\begin{tikzpicture}
\draw (0,0) circle(2);
\end{tikzpicture}}
 \lipsum[30]
\newpage

\InsertBoxR{3}{\begin{tikzpicture}
\draw (0,0) circle(2);
\end{tikzpicture}}
\lipsum[10]
\noindent
\lipsum[5]

\end{document} 

enter image description here

6
  • Thank you very much. I don't know the insbox.tex macro! Commented Jul 11, 2021 at 0:36
  • I compile success. Sometimes, the last line meets the figure or long distance. How can I fixe the problem. im.ge/i/Ozfgp Commented Jul 11, 2021 at 7:21
  • @NamTranLe: You can use the optional argument, which, contrary to LaTeX usual syntax, comes as the last argument. So, explicitly, I would use \InsertBoxR{0}{\begin{tikzpicture} … \end{tikzpicture}}[1] for the first figure, and [-1] for the second figure.
    – Bernard
    Commented Jul 11, 2021 at 7:55
  • Thank you very much. Commented Jul 11, 2021 at 11:17
  • I is wrong when there are two paragraphs wrapping the picture. How can I correct sir? Commented Jul 23, 2021 at 13:17

You must log in to answer this question.

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