将文本框放置在正文的右侧或左侧

将文本框放置在正文的右侧或左侧

我经常需要在页面的右侧或左侧放置一个包含少量文本的文本框,并将文本环绕在其周围。在此示例中,我尝试使用 wrapfig(尽管我没有图像或图形)。只有第一个文本框浮动在右侧。也许 wrapfig 不是正确的方法?

下面是示例代码和输出。 在此处输入图片描述

\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} 

答案1

我发现收件箱工作更可靠。希望借助它和 tcolorbox,我可以完成以前在 word 中所做的一切(在右侧或左侧显示一些信息的浮动框)。

\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}

在此处输入图片描述

答案2

这是根据我之前的回答,将段落环绕在与右(或左)边距对齐的框周围的解决方案。

不需要其他包裹。

该命令\InsertBox已定义,有 4 个参数。

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

  1. (可选)在文本区域中放置框的位置:rlr,right, 是默认值);
  2. 箱子里的东西;
  3. 所需的盒子宽度;
  4. 要成形的段落。

包装无额外包装

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}

相关内容