在 tikzpicture 环境中环绕文本

在 tikzpicture 环境中环绕文本

我一直在尝试使用考试环境为考试提供出色的解决方案。我希望解决方案包含一个使用tikzpicture和的图形,pgfplots然后让文本环绕图像。我可以使用以下方法使其在没有解决方案环境的情况下工作wrapfigure在此处输入图片描述

但是一旦我使用该solutions环境,图像就会放错位置: 在此处输入图片描述

以下是我一直使用的代码:

\documentclass[12pt]{exam}
\usepackage{pgfplots}
\usepackage{wrap fig}
\usepackage{lipsum}
\printanswers

\begin{document}
    \begin{questions}
    \question[10]
    Choose A or B.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{choices}
    \choice some problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{solution}

    \begin{wrapfigure}{L}{.5\textwidth}
        \begin{tikzpicture}
            \begin{axis}[
            smooth,
            trig format plots=rad,
            axis lines=middle,
            axis equal]
            \addplot [domain=0:pi, samples=150, red] ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)});
            \draw [-stealth, ultra thick, dashed, draw=blue] 
                    (axis cs: 0,0)
                    -- ++(canvas polar cs: radius=3cm,angle=0);
            \draw [-stealth, ultra thick, dashed, draw=blue] 
                (axis cs: 0,0)
                    -- ++(canvas polar cs: radius=4cm,angle=deg{pi/5});
            \end{axis}
        \end{tikzpicture}%
    \end{wrapfigure}
    \lipsum[1-1]
    \end{solution}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \choice some other problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{solution}
    another solution
    \end{solution}
    \end{choices}
    \end{questions}
\end{document}

如能提供任何提示/建议我将非常感激!

编辑

我应该提到我收到以下警告:

Package wrapfig Warning: wrapfigure used inside a conflicting environment on in
put line 17.

答案1

从概念上来说,更简单的方法是将文本放入小页面并手动将其拆分。 \nopar结束一个段落,但不会使其看起来像段落的结尾。

优点是您可以包含方程式和其他非\baselineskip类型字段而不会弄乱一切。只需将所有内容放入迷你页面,直到您准备好放置为止\nopar

\documentclass[12pt]{exam}
\usepackage{pgfplots}
\usepackage{wrap fig}
\usepackage{lipsum}
\printanswers

\newsavebox{\mybox}% exam uses box0, possibly others
\newcommand{\nopar}{\strut{\parfillskip=0pt \parskip=0pt \par}}

\begin{document}
    \begin{questions}
    \question[10]
    Choose A or B.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{choices}
    \choice some problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{solution}

    \savebox{\mybox}{% measure image
        \begin{tikzpicture}
            \begin{axis}[
            smooth,
            trig format plots=rad,
            axis lines=middle,
            axis equal]
            \addplot [domain=0:pi, samples=150, red] ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)});
            \draw [-stealth, ultra thick, dashed, draw=blue] 
                    (axis cs: 0,0)
                    -- ++(canvas polar cs: radius=3cm,angle=0);
            \draw [-stealth, ultra thick, dashed, draw=blue] 
                (axis cs: 0,0)
                    -- ++(canvas polar cs: radius=4cm,angle=deg{pi/5});
            \end{axis}
        \end{tikzpicture}}
    \par\noindent\raisebox{\dimexpr \ht\strutbox-\ht\mybox}{\usebox\mybox}\hfill
    \begin{minipage}[t]{\dimexpr \linewidth-\wd\mybox-\columnsep}
Lorem ipsum dolor sit amet, consectetuer adipiscinvestibulum ut, placerat ac, adipiscing vitae, felis. gravida mauris. Nam arcu libero, nonummy eget, cotate a, magna. Donec vehicula augue eu neque. Pmorbi tristique senectus et netus et malesuada famMauris ut leo. Cras viverra metus rhoncus sem. Nulum urna fringilla ultrices. Phasellus eu tellus sit placerat. Integer sapien est, iaculis in,\nopar
    \end{minipage}
\noindent pretium quPraesent eget sem vel leo ultrices bibendum. Aenedolor nulla, malesuada eu, pulvinar at, mollis ac, ntor semper nulla. Donec varius orci eget risus. Deu, accumsan eleifend, sagittis quis, diam. Duis egdignissim rutrum.

    \end{solution}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \choice some other problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{solution}
    another solution
    \end{solution}
    \end{choices}
    \end{questions}
\end{document}

答案2

您可以使用 中的通用代码insbox.tex来实现这一点。请注意,如果您在第一个段落后添加一个额外的段落,则insbox在环境中会以某种方式严重失败(因为它无法计算仍需要多少空间)。为此,您可以使用(但前提是您的段落在插入后开始)。不过,当您的新段落在插入过程中开始时,我没有创建任何可以工作的东西。solution\restoreinsbox

编辑:我添加了两个宏\restoreinsboxL\restoreinsboxR,它们都带有一个可选参数。如果没有给出,它们的行为就像,但如果你给它们一个可选参数,它将被解释为额外的行数,这些行数应该很短。这样,如果第一段比插入的框短,\restoreinsbox你也可以恢复和的效果\InsertBoxL\InsertBoxR

\documentclass[12pt]{exam}
\usepackage{pgfplots}
\usepackage{duckuments}
\printanswers
\input{insbox} % this is no LaTeX package, but a generic one

\newcommand*\restoreinsboxL[1][0]
  {%
    \everypar{}%
    \ifnum#1=0
      \parshape=0
    \else
      \ParShape=2
                {#1} {\the\@framewidth} 0pt
                1    0pt                0pt
    \fi
  }
\newcommand*\restoreinsboxR[1][0]
  {%
    \everypar{}%
    \ifnum#1=0
      \parshape=0
    \else
      \ParShape=2
                {#1} 0pt {\the\@framewidth}
                1    0pt 0pt
    \fi
  }
\newcommand*\restoreinsbox
  {%
    \everypar{}%
    \parshape=0
  }

\begin{document}
    \begin{questions}
    \question[10]
    Choose A or B.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{choices}
    \choice some problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{solution}

    \InsertBoxL{0}
      {%
        \begin{tikzpicture}
            \begin{axis}[
            smooth,
            trig format plots=rad,
            axis lines=middle,
            axis equal]
            \addplot [domain=0:pi, samples=150, red] ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)});
            \draw [-stealth, ultra thick, dashed, draw=blue] 
                    (axis cs: 0,0)
                    -- ++(canvas polar cs: radius=3cm,angle=0);
            \draw [-stealth, ultra thick, dashed, draw=blue] 
                (axis cs: 0,0)
                    -- ++(canvas polar cs: radius=4cm,angle=deg{pi/5});
            \end{axis}
        \end{tikzpicture}%
      }%
    \blindduck[1]
    \par
    \restoreinsbox
    \blindduck[2]
    \end{solution}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \choice some other problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{solution}
    another solution
    \end{solution}
    \end{choices}
    \end{questions}
\end{document}

在此处输入图片描述

编辑2:下面定义了另一个包装宏实现的相当原始的草稿。它不能做很多事情,可能需要大量手动调整。然而,它在以下示例中运行得相当好。

它定义了两个宏,都只在本地范围内起作用。第一个是

\wrapcontents[<width>]{<l/r>}[<correction>]{<contents>}[<lines to skip>]

如果您指定一个<width>参数,则 width 将用于框宽度(并且材料将以垂直模式排版,类似于\parbox)。l/r您可以使用 来选择放置内容的位置,在l左边还是r右边。<correction>可以是无符号的正数,在这种情况下行数必须更短,也可以是有符号的整数(+/-),在这种情况下,它将添加到自动计算的行数中。<contents>是应该包装的内容,可以读作\vbox\hbox<lines to skip>是插入前段落应正常排版的行数。

第二个宏是

\restorewrap[<lines>]

如果您没有指定<lines>缩进结束的效果,如果您指定一个数字,则该效果也将用于接下来的<lines>几行。

我不知道为什么,但你必须在环境\restorewrap内使用choices,这在普通文本中不是这样的(我懒得去研究)。但由于此实现不会自动对后续段落执行任何操作,并且仅在本地范围内起作用,因此它不会影响下一个\choice

免责声明:部分代码(插入的输出)受到了 的极大启发insbox.tex

完成 MWE:

\documentclass[12pt]{exam}
\usepackage{pgfplots}
\usepackage{duckuments}
\printanswers

\usepackage{grabbox}

\makeatletter
\newlength\wrapcont@width
\newlength\wrapcont@sep
\newlength\wrapcont@indent
\newlength\wrapcont@hsize
\newcount\wrapcont@lines
\newcount\wrapcont@tempcount
\setlength\wrapcont@sep{5pt}
\newsavebox\wrapcont@box
\newcommand*\wrapcontents[2][]
  {%
    % #1 : width
    % #2 : l/r
    \@ifnextchar[{\wrapcont@a{#1}{#2}}{\wrapcont@a{#1}{#2}[+0]}%
  }
\def\wrapcont@a#1#2[#3]%
  {%
    % #1 : width
    % #2 : l/r
    % #3 : correction
    \if\relax\detokenize{#1}\relax
      \wrapcont@hbox
    \fi
    \wrapcont@vbox{#1}{#2}{#3}%
  }
\def\wrapcont@hbox\fi\wrapcont@vbox#1#2#3%
  {%
    % #1 : width
    % #2 : l/r
    % #3 : correction
    \fi
    \@grabbox{}\wrapcont@box{}\hbox{}{\wrapcont@b{#2}{#3}}%
  }
\newcommand*\wrapcont@vbox[3]
  {%
    % #1 : width
    % #2 : l/r
    % #3 : correction
    \@grabbox{\hsize=#1\relax\linewidth\hsize}\wrapcont@box{}\vbox{}%
      {\wrapcont@b{#2}{#3}}%
  }
\newcommand*\wrapcont@b[2]
  {%
    % #1 : l/r
    % #2 : correction
    \@ifnextchar[{\wrapcont@c{#1}{#2}}{\wrapcont@c{#1}{#2}[0]}%
  }
\newcommand*\wrapcont@parshape{}
\def\wrapcont@c#1#2[#3]%
  {%
    % #1 : l/r
    % #2 : correction
    % #3 : lines skipped
    \wrapcont@width\wd\wrapcont@box\relax
    \advance\wrapcont@width\wrapcont@sep
    \wrapcont@hsize\linewidth
    \advance\wrapcont@hsize-\wrapcont@width
    \ifnum0<0#2\relax
      \wrapcont@lines#2\relax
    \else
      \wrapcont@lines
        \numexpr
          (\ht\wrapcont@box+\dp\wrapcont@box\ifnum#3>0 +\wrapcont@sep\fi+\wrapcont@sep)%
          /\baselineskip#2%
        \relax
    \fi
    \vbox to 0pt
      {%
        \vskip#3\baselineskip
        \ifnum#3>0
          \vskip\wrapcont@sep
        \fi
        \def\@tempa{#1}%
        \def\@tempb{r}%
        \ifx\@tempa\@tempb
          \vtop{\noindent\hbox to\hsize{\hss\usebox\wrapcont@box}}%
        \else
          \def\@tempb{l}%
          \ifx\@tempa\@tempb
          \else
            \GenericError
              {}
              {wrapcontent Error: Unknown alignment '#1', using 'l' as backup.}
              {}{}%
          \fi
          \vtop{\noindent\usebox\wrapcont@box}%
        \fi
        \vss
      }%
    \begingroup
      \def\@tempa{#1}%
      \def\@tempb{r}%
      \ifx\@tempa\@tempb
        \aftergroup\wrapcont@indent\aftergroup\z@
      \else
        \aftergroup\wrapcont@indent\aftergroup\wrapcont@width
      \fi
    \endgroup
    \vglue-\parskip
    \vskip-\baselineskip
    \wrapcont@tempcount0
    \edef\wrapcont@parshape{=\the\numexpr#3+\wrapcont@lines+1\relax\space}%
    \loop\ifnum#3>\wrapcont@tempcount
      \advance\wrapcont@tempcount\@ne
      \edef\wrapcont@parshape
        {\unexpanded\expandafter{\wrapcont@parshape}0pt \the\hsize\space}%
    \repeat
    \wrapcont@tempcount0
    \loop\ifnum\wrapcont@lines>\wrapcont@tempcount
      \advance\wrapcont@tempcount\@ne
      \edef\wrapcont@parshape
        {%
          \unexpanded\expandafter{\wrapcont@parshape}%
          \the\wrapcont@indent\space\the\wrapcont@hsize\space
        }%
    \repeat
    \edef\wrapcont@parshape
      {\unexpanded\expandafter{\wrapcont@parshape}0pt \the\hsize\space}
    \expandafter\parshape\wrapcont@parshape\relax
  }
\newcommand*\restorewrap[1][0]
  {%
    \ifnum0=#1\relax
      \parshape=0
    \else
      \wrapcont@tempcount0
      \edef\wrapcont@parshape{=\the\numexpr#1+1\relax\space}%
      \loop\ifnum#1>\wrapcont@tempcount
        \advance\wrapcont@tempcount\@ne
        \edef\wrapcont@parshape
          {%
            \unexpanded\expandafter{\wrapcont@parshape}%
            \the\wrapcont@indent\space\the\wrapcont@hsize\space
          }%
      \repeat
      \edef\wrapcont@parshape
        {\unexpanded\expandafter{\wrapcont@parshape}0pt \the\hsize\space}%
      \expandafter\parshape\wrapcont@parshape
    \fi
    \relax
  }
\makeatother

\begin{document}
    \begin{questions}
    \question[10]
    Choose A or B.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{choices}
    \choice some problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{solution}

    \wrapcontents{l}
      {%
        \begin{tikzpicture}
            \begin{axis}[
            smooth,
            trig format plots=rad,
            axis lines=middle,
            axis equal]
            \addplot [domain=0:pi, samples=150, red] ({2*sin(5*x)*cos(x)}, {2*sin(5*x)*sin(x)});
            \draw [-stealth, ultra thick, dashed, draw=blue] 
                    (axis cs: 0,0)
                    -- ++(canvas polar cs: radius=3cm,angle=0);
            \draw [-stealth, ultra thick, dashed, draw=blue] 
                (axis cs: 0,0)
                    -- ++(canvas polar cs: radius=4cm,angle=deg{pi/5});
            \end{axis}
        \end{tikzpicture}%
      }%
    \blindduck[3]
    \par
    \restorewrap[2]
    \blindduck[2]
    \par
    \restorewrap
    \blindduck[3]
    \end{solution}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \choice some other problem
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \begin{solution}
    another solution
    \end{solution}
    \end{choices}
    \end{questions}
\end{document}

结果:

在此处输入图片描述

相关内容