3

I'm trying to create some exercises along with sample solutions. For each solution I want a small table on the left with the data from the exercise description and the actual calculations plus explanations for them on the right.

To achieve this I'm using the wrapfig package. Unfortunately the solution doesn't wrap to the right of the data table – it instead appears above the table, under the exercise description.

How do I get the solution to actually wrap the data table, instead of jumping above it?

MWE:

% !TeX program = xelatex
\documentclass{article}

\usepackage{wrapfig}

\newenvironment{data}[1]%
{\begin{wraptable}{l}{#1}\begin{tabular}{|l|}\hline}%
        {\hline\end{tabular}\end{wraptable}}
\newcommand{\goals}{\hline}

\begin{document}
    \section{Calculating}
    \subsection{Addition}
    $\alpha=30$° and $\beta=15$°. Divide $\alpha$ with $\beta$!

    \begin{data}{2cm}
        $\alpha=30$°\\
        $\beta=15$°\\
        \goals
        $\gamma=?$\\
    \end{data}

    This is the formula:

    $\gamma=\frac{\alpha}{\beta}$

    This is the actual calculation:

    $\gamma=\frac{30°}{15°}=2$°

    Hopefully this was easy!
\end{document}

Result:

image showing the output of the MWE

Mockup of the desired result:

image showing the rough desired output

tambre
  • 223
  • 1
  • 11

1 Answers1

3

As explained by Christian Hupfer in this answer, the wrap environments are not real environments. So you need to work with \beginwraptable and \endwraptable instead. I also used the opportunity to replace the degree symbols by the corresponding \si syntax.

\documentclass{article}
\usepackage{siunitx}

\usepackage{wrapfig}

\newenvironment{data}%
{\wraptable{l}{0pt}% 
\begin{tabular}{|l|}\hline}%
        {\hline\end{tabular}\endwraptable}
\newcommand{\goals}{\hline}

\setlength\intextsep{0pt}% from https://tex.stackexchange.com/a/247109/121799
\begin{document}
    \section{Calculating}
    \subsection{Addition}
    $\alpha=\ang{30}$ and $\beta=\ang{15}$. Divide $\alpha$ with $\beta$!

    \begin{data}
        $\alpha=\ang{30}$\\
        $\beta=\ang{15}$\\
        \goals
        $\gamma=?$\\
    \end{data}
    This is the formula:

    $\gamma=\frac{\alpha}{\beta}$

    This is the actual calculation:

    $\gamma=\frac{\ang{30}}{\ang{15}}=\ang{2}$

    Hopefully this was easy!
\end{document}

enter image description here

NOTE: I was unable to set \setlength\intextsep{0pt} locally as in this answer.

  • Surely \SI{30}{\degree} should be used instead? – Troy May 12 '18 at 18:07
  • That indeed helps with getting the text to the right. Is there anything that could be done to get the text on the right to align with the top of the table on the left, though? – tambre May 12 '18 at 18:14
  • @marmot Yes? Sorry I don't see what you're getting at. It works, but it's wrong. The argument for \si is only meant to be units, not numbers. But yes, \ang works too. – Troy May 12 '18 at 18:33
  • @Troy No worries. This just happens to be a question on \wraptable, not siunitx, and the outputs of all three options are the same (AFAIK). Anyway, thanks for correcting that! –  May 12 '18 at 18:35
  • @tambre I posted something that removes the vertical space. –  May 12 '18 at 18:36
  • @marmot Thanks! \setlength\intextsep{0pt} from that question seems to be the better choice to ensure proper wrapping after the table vs the vspace method. – tambre May 12 '18 at 18:40
  • @tambre I fully agree, but that give you the desired result? –  May 12 '18 at 18:47
  • @marmot It does give the desired result. I also now noticed, that I can get the solution text to be spaced correctly automatically, by simply specifying the width parameter for the wraptable as 0 units of whatever – no need for the annoying environment parameter! – tambre May 12 '18 at 18:51
  • @tambre The second thing I knew, I was under the impression you do that on purpose to have a larger distance. I am still struggling to reproduce the first thing, though, but most likely I am doing something stupid. –  May 12 '18 at 18:59