0

I frequently need to position a text box containing a small amount of text on either the right or left of the page, and wrap text around it. In this example I attempted to use wrapfig (though i have no image or figures). Only the first text box floats on the right. Perhaps wrapfig is not the correct approach?

Sample code and ouptut below. enter image description here

\documentclass[12pt]{article}
\usepackage{lipsum}
\usepackage{color}
\usepackage{tcolorbox}
\usepackage{wrapfig}

\newtcolorbox{mybox}{colback=white,colframe=black,sharp corners} %framed box

\newcommand{\mytext}{ \lipsum[1] }

\newenvironment{icfloatbox} %A box to float text around the left of. {\begin{wrapfigure}{R}{2cm}\begin{mybox} another box in the icfloatbox.} {\end{mybox} \end{wrapfigure}}

\begin{document}

%works \begin{wrapfigure}{R}{5cm} \begin{mybox} Expected on the right 1\end{mybox} \end{wrapfigure} \mytext% \begin{wrapfigure}{R}{5cm} \begin{mybox} Expected on the right 2\end{mybox} \end{wrapfigure} \mytext% \begin{wrapfigure}{R}{5cm} \begin{mybox} Expected on the right 3\end{mybox} \end{wrapfigure} \mytext%

\end{document}

  • Uhm, your code works fine for me. (However, I've got an old system.) – campa Nov 18 '21 at 16:46
  • @campa so all your boxes are positioned on the right the page? that is what I am attempting in the example. – Doug Ransom Nov 18 '21 at 17:04
  • The results are the same with pdflatext or xelatex. both at version 3.141592653 – Doug Ransom Nov 18 '21 at 17:06
  • Yes, I get all boxes placed on the right with the text wrapping around them. But as I said I've got an older system, so newer updates might have broken something... – campa Nov 18 '21 at 17:08
  • If I leave an empty line between \mytext and \begin{wrapfigure} second and third boxes are shown on the right. (I'm using pdflatex with a yesterday updated miktex system) – Ignasi Nov 18 '21 at 18:19
  • @ignasi that made no difference in my environment. – Doug Ransom Nov 18 '21 at 18:30

2 Answers2

1

I found insbox worked more reliably. Hopefully with that and tcolorbox I can do everything i used to do in word (floating boxes with some info to the right or left).

\documentclass[12pt]{article}
\usepackage{lipsum}
\input{insbox}
\usepackage{color}
\usepackage{tcolorbox}

\newtcolorbox{mybox}{colback=white,colframe=black,sharp corners,width=3in} %framed box

\newcommand{\dobox} {\begin{mybox}Box box box\end{mybox}}

\begin{document}

\dobox ok blah blah \InsertBoxR{5}{\dobox} \lipsum[1] \InsertBoxR{2}{\dobox} the end. \lipsum[2] \end{document}

enter image description here

0

This is solution to wrap a paragraph around a box flushed to the right (or left) margin based on my previous answer.

No other packages were needed.

The command \InsertBox was defined, with 4 parameters.

\InsertBox[<l or r (default} >]{<box content>}{<box width>}{<text to be shaped>}

  1. (optional) where to place the box in the text area: r or l (r,right, is the default);
  2. content of the box;
  3. desired box width;
  4. paragraph to be shaped.

See wrapping without extra packages

a

\documentclass{article}

\usepackage{kantlipsum}% dummy text

%*************************************************************************************************** \newlength{\AboveSkipbox} \setlength{\AboveSkipbox}{0.7\baselineskip} % above space <<<<

\newlength{\Sidesepbox} \setlength{\Sidesepbox}{1.5em} % side space <<<

\newlength{\BoxHeight} \newlength{\ParHeight}
\newlength{\FirstIndent} \newlength{\FigureBoxWidth} \newlength{\FigIndent}
\newsavebox{\mybox} \newsavebox{\tmpbox} %% From https://tex.stackexchange.com/questions/127504/more-condensed-version-of-parshape \makeatletter \let\SavedIndent\parindent
\let@afterindentfalse@afterindenttrue @afterindenttrue \def\newparshape{\parshape@npshape0{}} \def@npshape#1#2#3{\ifx\#3\expandafter@@@npshape\else\expandafter@@npshape\fi {#1}{#2}{#3}} \def@@npshape#1#2#3#4#5{% \ifnum#3>\z@\expandafter@firstoftwo\else\expandafter@secondoftwo\fi {\expandafter@@npshape\expandafter{\the\numexpr#1+1\relax}{#2 #4 #5}{\numexpr#3-1\relax}{#4}{#5}}% {@npshape{#1}{#2}}} \def@@@npshape#1#2#3{#1 #2} \def\SetFirstIndent{\if@afterindent\FirstIndent\SavedIndent\else\FirstIndent\z@\fi} \makeatother

\newcommand{\nlines}[1]{\the\numexpr#1/\baselineskip +1 \relax}
\newcommand{\TypesetBox}[1]{\makebox[\linewidth][#1]{\raisebox{\dimexpr-\ht\mybox-\AboveSkipbox\relax}[0pt][0pt]{\usebox{\mybox}}}}
\newcommand{\TypesetTextShortLong}[1]{% \hspace*{\parindent} \ifdim\dimexpr\ht\tmpbox+\dp\tmpbox<\dimexpr\ht\mybox+\dp\mybox+2\baselineskip\relax%short \hspace{-\parindent}\usebox{\tmpbox}\vspace{\dimexpr\ht\mybox+\dp\mybox+\AboveSkipbox-\ht\tmpbox-\dp\tmpbox\relax} \else%long #1\fi }

\newcommand{\MakeFigureSpace}[1]{% \ifx#1l% left \setlength{\FigIndent}{\FigureBoxWidth} \else% right \setlength{\FigIndent}{0pt} \fi }

\newcommand{\InsertBox}[4][r]{% \SetFirstIndent%
\setlength{\FigureBoxWidth}{\dimexpr#3+\Sidesepbox\relax}
\sbox{\tmpbox}{\parbox[t]{\dimexpr\linewidth-\FigureBoxWidth\relax}{\hspace{\FirstIndent}#4}} \sbox{\mybox}{\fbox{\parbox{#3}{#2}}}% \MakeFigureSpace{#1}% Figure Indent = \parindent or Fig Box width \vspace{-\baselineskip} \hspace{\dimexpr-\FigIndent-\parindent\relax}\TypesetBox{#1}
\newparshape% shape paragraph text {\nlines{\dimexpr\ht\mybox+\dp\mybox+\baselineskip\relax}}{\FigIndent}{\dimexpr\linewidth-\FigureBoxWidth\relax}% {1}{0pt}{\linewidth}\ \TypesetTextShortLong{#4} } %***************************************************************************************************

\begin{document}

\InsertBox{Expected on the right 1}{0.3\textwidth}{\kant[2]}

\kant[9]

\InsertBox[l]{\bfseries \large As I have shown elsewhere, Aristotle tells
    us that the objects in space and time, in the full sense of these
    terms, would be falsified.}{0.4\textwidth}{\kant[4]}

\end{document}

Simon Dispa
  • 39,141