0

I'm looking to have pseudocode in my document placed nicely by LaTeX (ie. no exiting bounding box, no overlapping other text, etc.), while having text wrap around the pseudocode (as the width of text is small).

This question answers how to wrap text around pseudocode. However, my pseudocode goes beyond the bottom of the page and overlays on the footnote (if I change r to R, it overlays on a figure). How would I ensure this does not happen, beyond manually placing the pseudocode at a different point in my LaTeX document (ie. Section 1, Section 2, Section 2 code -> Section 1, Section 2 code, Section 2 to solve the problem would be not good because I have to submit the LaTeX document)

My current code:

\begin{wrapfigure}{r}{0.39\textwidth}
\begin{minipage}{0.39\textwidth}
\begin{algorithm}[H]
    \caption{example}\label{alg:example},
    \begin{algorithmic}[1]
        % algorithm
    \end{algorithmic}
\end{algorithm}
\end{minipage}
\end{wrapfigure}
  • The approach in the linked answer is manual, as you're forcing the pseudocode to coincide with the start of a specific paragraph. So, movement elsewhere will have to be manual. One option is to check whether a page beak is necessary and issue it if the pseudocode would extend beyond the text block. However, in some cases, this may cause the preceding page to be underfull. So, be specific in terms of what you want and update the question accordingly. – Werner Sep 25 '23 at 21:45
  • @Werner - updated question. The idea is that I want text wrapped around pseudocode but not the weird placement behavior. If I change H to any other (h, b, t), I get errors of "Missing number, treated as zero; LaTeX Error: Not in outer par mode; Undefined control sequence", which do not occur with H. – user760900 Sep 25 '23 at 22:08
  • 1
    Sure. Changing to h, b or t makes the image float, which it can't, since wrapfigure wants to keep it in place. You should provide more detail on how things should function around the page boundary; mentioning "I don't want the weird placement" doesn't describe whether the text flow should remain and (say), the pseudocode should float to the top of the following page without wrapping, or with wrapping. Please be specific here as well. – Werner Sep 25 '23 at 22:19
  • @Werner - Apologies, I'm relatively new to latex and I'm not sure I understand what you mean. I want the behavior of hb, but with text wrapping. I want the pseudocode to remain within the bounding box. I'm ok with the pseudocode going to the next page, but I would still like wrapping there. I do not want a page/section of just the pseudocode with unwrapped text (the pseudocode is on page 2 of a ~10 page doc, so this should not be a concern unless we explicitly push it to an empty page). Please let me know if this is still unclear and I will attempt to clarify – user760900 Sep 25 '23 at 22:32
  • With that requirement you're going to have to intervene manually, since your requirements are to maintain some form of text wrapping. Can you create an anonomized minimal example (we call that an MWE) that replicates the problematic behaviour you're currently experiencing? – Werner Sep 25 '23 at 22:38
  • @Werner - I've tried to do that with my code in the question. I've been trying to replicate the overlap behavior, but I can't seem to do that with anonymization in a reasonable length. Even if I move the algorithm location, there seems to be overlap with other images/figures – user760900 Sep 25 '23 at 22:48
  • Since your are automatically in a figure environment, uou can use \captionof{algorithm}{...} and lose both the minipage and the algorithm environments. The bottom of the page is another problem altogether. – John Kormylo Sep 26 '23 at 03:34

1 Answers1

2

You have a couple of options here, not really automated in any way. Let's start off with something that replicates your current issue:

enter image description here

\documentclass{article}

\usepackage{wrapfig,graphicx} \usepackage{lipsum,showframe}

\begin{document}

\lipsum[1-4]

\begin{wrapfigure}{r}{0.4\linewidth} \includegraphics[width=0.9\linewidth]{example-image-10x16} \end{wrapfigure}% Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam consectetur arcu at tellus consequat molestie. Sed feugiat turpis ac massa vestibulum feugiat. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean pharetra tortor est, ut hendrerit diam condimentum ut. Nunc consequat pulvinar posuere. Curabitur tincidunt vel est at rhoncus. Nunc sagittis lacus elit, nec malesuada magna lacinia eget. Proin porta, justo eu molestie sagittis, risus lacus sollicitudin quam, ut fermentum mauris augue at turpis. Donec auctor consequat metus quis fringilla. Nulla nec enim vel libero rhoncus pellentesque. Fusce nec lacus orci. Pellentesque eget lectus vitae tortor consequat imperdiet. Suspendisse aliquet blandit dolor, bibendum commodo ligula cursus et. Mauris aliquet massa in est facilisis, sed efficitur ipsum iaculis. Nulla vehicula diam mauris, a sagittis odio sodales et. Aliquam imperdiet feugiat lacus sed condimentum. Cras tellus turpis, dapibus eu tempor eget, lacinia et nulla. Nunc tempus malesuada massa ut rhoncus. Morbi vulputate sapien eros, at facilisis nulla tempus sed. Etiam tincidunt elit at molestie aliquet.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam consectetur arcu at tellus consequat molestie. Sed feugiat turpis ac massa vestibulum feugiat. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean pharetra tortor est, ut hendrerit diam condimentum ut. Nunc consequat pulvinar posuere. Curabitur tincidunt vel est at rhoncus. Nunc sagittis lacus elit, nec malesuada magna lacinia eget. Proin porta, justo eu molestie sagittis, risus lacus sollicitudin quam, ut fermentum mauris augue at turpis. Donec auctor consequat metus quis fringilla. Nulla nec enim vel libero rhoncus pellentesque. Fusce nec lacus orci. Pellentesque eget lectus vitae tortor consequat imperdiet. Suspendisse aliquet blandit dolor, bibendum commodo ligula cursus et. Mauris aliquet massa in est facilisis, sed efficitur ipsum iaculis. Nulla vehicula diam mauris, a sagittis odio sodales et. Aliquam imperdiet feugiat lacus sed condimentum. Cras tellus turpis, dapibus eu tempor eget, lacinia et nulla. Nunc tempus malesuada massa ut rhoncus. Morbi vulputate sapien eros, at facilisis nulla tempus sed. Etiam tincidunt elit at molestie aliquet.

\end{document}

A couple of things to note in the above example:

  • I've used the lowercase right option to wrapfigure, which places it here, rather than Right, which allows it to float.

  • The wrapfig documentation explicitly mentions the following:

    The environment should be placed so as to not run over a page break.

This is exactly where you have it.

  • I've inserted a tall image (example-image-10x16) instead of an algorithm/pseudocode. The process/application is the same in either case, so the suggested solutions below work in both cases.

Solution 1

Use the Right (floating) placement specifier for the wrapfigure environment. This allows the block to be wrapped around to float to the following paragraph, which is on a subsequent page.

enter image description here

...
\begin{wrapfigure}{R}{0.4\linewidth}
  \includegraphics[width=0.9\linewidth]{example-image-10x16}
\end{wrapfigure}%
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam consectetur arcu at tellus consequat molestie. 
...

Solution 2

Set the document without the oddly-placed wrapfigure. This will allow you to see where the text ends on that specific page. Then, insert a \parnopar exactly there so you can allow the paragraph to end (pushing content to be flush right with the bottom of the now-separated paragraph. Insert a paragraph break (a vertical gap/blank line in your code) and then insert the wrapfigure with a new paragraph starting with \noindent.

In the first solution we can see that the closing words on the first page is "... dolor, bibendum commodo". So, we insert the \parnopar exactly after commodo:

dolor, bibendum commodo \parnopar

\begin{wrapfigure}{r}{0.4\linewidth} \includegraphics[width=0.9\linewidth]{example-image-10x16} \end{wrapfigure}% \noindent ligula cursus et.

The blank line will force a page break, while \noindent will make sure that the page break doesn't seem like a paragraph break and set the following text - "ligula cursus et." - flush to the left margin.

enter image description here

\documentclass{article}

\usepackage{wrapfig,graphicx} \usepackage{lipsum,showframe}

\newcommand{\parnopar}{\parfillskip=0pt\par\parskip=0pt\noindent}

\begin{document}

\lipsum[1-4]

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam consectetur arcu at tellus consequat molestie. Sed feugiat turpis ac massa vestibulum feugiat. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean pharetra tortor est, ut hendrerit diam condimentum ut. Nunc consequat pulvinar posuere. Curabitur tincidunt vel est at rhoncus. Nunc sagittis lacus elit, nec malesuada magna lacinia eget. Proin porta, justo eu molestie sagittis, risus lacus sollicitudin quam, ut fermentum mauris augue at turpis. Donec auctor consequat metus quis fringilla. Nulla nec enim vel libero rhoncus pellentesque. Fusce nec lacus orci. Pellentesque eget lectus vitae tortor consequat imperdiet. Suspendisse aliquet blandit dolor, bibendum commodo \parnopar

\begin{wrapfigure}{r}{0.4\linewidth} \includegraphics[width=0.9\linewidth]{example-image-10x16} \end{wrapfigure}% \noindent ligula cursus et. Mauris aliquet massa in est facilisis, sed efficitur ipsum iaculis. Nulla vehicula diam mauris, a sagittis odio sodales et. Aliquam imperdiet feugiat lacus sed condimentum. Cras tellus turpis, dapibus eu tempor eget, lacinia et nulla. Nunc tempus malesuada massa ut rhoncus. Morbi vulputate sapien eros, at facilisis nulla tempus sed. Etiam tincidunt elit at molestie aliquet.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam consectetur arcu at tellus consequat molestie. Sed feugiat turpis ac massa vestibulum feugiat. Orci varius natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Aenean pharetra tortor est, ut hendrerit diam condimentum ut. Nunc consequat pulvinar posuere. Curabitur tincidunt vel est at rhoncus. Nunc sagittis lacus elit, nec malesuada magna lacinia eget. Proin porta, justo eu molestie sagittis, risus lacus sollicitudin quam, ut fermentum mauris augue at turpis. Donec auctor consequat metus quis fringilla. Nulla nec enim vel libero rhoncus pellentesque. Fusce nec lacus orci. Pellentesque eget lectus vitae tortor consequat imperdiet. Suspendisse aliquet blandit dolor, bibendum commodo ligula cursus et. Mauris aliquet massa in est facilisis, sed efficitur ipsum iaculis. Nulla vehicula diam mauris, a sagittis odio sodales et. Aliquam imperdiet feugiat lacus sed condimentum. Cras tellus turpis, dapibus eu tempor eget, lacinia et nulla. Nunc tempus malesuada massa ut rhoncus. Morbi vulputate sapien eros, at facilisis nulla tempus sed. Etiam tincidunt elit at molestie aliquet.

\end{document}


With Solution 2 above in mind, you could also cut your algorithm in half, showing some of it at the bottom of one page, and having that follow through with the lower half shown at the top of the subsequent page (see Algorithm over 2 pages). However, breaking up such a display across pages of a possibly-printed output would be odd (so not advised).

Werner
  • 603,163
  • 1
    I tried solution 1, but it seems to put it underneath another image (figure* with option h, centering, and 3 subfigure with option b, centering, label, and caption). – user760900 Sep 26 '23 at 01:36
  • I think solution 1 is more what I was going for in that it is automatically typeset such that if the content changes, the figure does not have to manually be adjusted. However, I'm unsure how to solve the above. If I change the figure* to H, that seems to work, but I don't want to change all figures to H if I can help it – user760900 Sep 26 '23 at 05:00
  • @user760900: You never mentioned you use figure* (yes, that's something completely different to figure or wrapfigure). You mention you have a ~10-page document. There can't be that many figures that changing elements isn't impossible. And sure, everyone wants stuff to just automatically work a different way. However, as mentioned in the answer, your use falls under the tips [to avoid] for good placement. So, necessarily there is little accommodation naturally for that in the package, other than R or L placements. – Werner Sep 26 '23 at 05:41