Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for wrapfigure #376

Open
kuon opened this issue Mar 1, 2022 · 15 comments
Open

Support for wrapfigure #376

kuon opened this issue Mar 1, 2022 · 15 comments
Labels
enhancement New feature or request latex LaTeX engines related libraries and technologies
Milestone

Comments

@kuon
Copy link

kuon commented Mar 1, 2022

I have some use case where I need the figure to be wrapped within the text.

I can achieve this with:

\begin{wrapfigure}{R}{6cm}
\centering
\includegraphics[width=5.5cm]{a.jpg}
\end{wrapfigure}

but it would be very handy to have it natively supported for both HTML and pdf output.

HTML could simple use float css property.

@dragonstyle
Copy link
Collaborator

This is a great suggestion and something that we'll plan on supporting in the future. Thanks!

dragonstyle added a commit that referenced this issue Mar 2, 2022
When a supplemental file is contributed to a render, it can now includes foramts that go along with it (so that it won’t be forced to render in an unsupported format). For example, when a listing gets added to the render of a revealjs document, it still needs to render as an html document rather than as a revealjs presentation.

#376
dragonstyle added a commit that referenced this issue Mar 2, 2022
When a supplemental file is contributed to a render, it can now includes foramts that go along with it (so that it won’t be forced to render in an unsupported format). For example, when a listing gets added to the render of a revealjs document, it still needs to render as an html document rather than as a revealjs presentation.

#376
dragonstyle added a commit that referenced this issue Mar 2, 2022
When a supplemental file is contributed to a render, it can now includes foramts that go along with it (so that it won’t be forced to render in an unsupported format). For example, when a listing gets added to the render of a revealjs document, it still needs to render as an html document rather than as a revealjs presentation.

#376
@jmbuhr
Copy link
Contributor

jmbuhr commented Mar 3, 2022

Just for reference and in case you want to take this even further: With html / css text can also wrap text around the outlines of a transparent png, which I used in this quarto-made scientific poster: https://hits-mbm-dev.github.io/paper-talin-loop/poster.html

image

(css: https://github.com/hits-mbm-dev/paper-talin-loop/blob/a4f94a20e1e88d72585da36727a3c424de673a3d/poster.css#L23-L33)

@db369
Copy link

db369 commented May 12, 2022

Is there a way to do this with PDF (via RStudio IDE)? Using 'float', it looks ok in the visual editor, but not when rendered... I copied syntax from hello.qmd, which also doesn't render correctly in PDF (which I discovered later...).

@jmbuhr
Copy link
Contributor

jmbuhr commented May 12, 2022

No, this is a feature of html and css. There may be a LaTeX package that does something similar for pdf but I am no export on TeX.

@jjallaire jjallaire added this to the Future milestone Jun 9, 2022
@mcanouil mcanouil added latex LaTeX engines related libraries and technologies enhancement New feature or request labels Aug 16, 2023
@srvanderplas
Copy link

Any progress on this? It would be very useful to be able to wrap figures in PDF files for e.g. grant applications and other reports where pages are limited.

@mcanouil
Copy link
Collaborator

mcanouil commented Jun 20, 2024

An open issue means it's still planed.
Updates are made in the issues.
If you don't see anything, then that means there was no updates to provide.

Note that you can add a reaction on the main post to show your interest.

Anyhow, thanks for your interest in this issue and Quarto!

@JonasLoos
Copy link

As this is the first thing that shows up when searching for "quarto wrapfigure", I thought it might be helpful to share a workaround to get code block plots to be behave as a wrapfigure for Latex PDF output. I wrote a small lua filter to temporarily redefine the figure environment when the wrapfigure property is defined in the code block properties.

Click to see code and example

Lua filter:

if FORMAT:match 'latex' then
  function Div(elem)
    -- Check if this div has wrapfigure attribute
    if elem.attributes and elem.attributes["wrapfigure"] then
      local wrap_pos, wrap_width = elem.attributes["wrapfigure"]:match("^%s*(%S+)%s+(%S+)%s*$")
      if wrap_pos == nil or wrap_width == nil then
        io.stderr:write("Error: wrapfigure attribute must be of the form 'pos width', e.g. 'R 0.5'. Found: '" .. elem.attributes["wrapfigure"] .. "'\n")
        return elem
      end
      return {
          pandoc.RawInline('latex', '\\let\\oldfigure\\figure\n\\let\\endoldfigure\\endfigure\n\\renewenvironment{figure}{}{}\n\\begin{wrapfigure}{' .. wrap_pos.. '}{' .. wrap_width .. '\\textwidth}\\centering\\captionsetup{format=plain, labelformat=simple}'),
          elem,
          pandoc.RawInline('latex', '\\end{wrapfigure}\\let\\figure\\oldfigure\n\\let\\endfigure\\endoldfigure')
      }
    end
    return elem
  end
end

Save this somewhere (e.g. under assets/wrapfigure.lua) and reference it in the file metadata or _quarto.yml like this:

filters:
  - assets/wrapfigure.lua

Usage:

```{python}
#| label: fig-test123
#| fig-cap: Some caption
#| wrapfigure: R 0.5

import matplotlib.pyplot as plt
plt.plot([1,2,3])
plt.show()
```

Here R is the position (R -> right, L -> left), and 0.5 is the width, relative to \textwidth.

@cscheid
Copy link
Collaborator

cscheid commented Dec 11, 2024

That's a good workaround, thanks for sharing!

I'll note here that Quarto does support fig-env as an attribute to control the actual environment used for the figure. With that said, I unfortunately don't think that this would work with wrapfigure, because of the different required parameters.

@gordonwoodhull Do you think we could add something like https://sitandr.github.io/typst-examples-book/book/packages/wrapping.html to our Typst renderer?

I would be happy with shipping native wrapfigure support in Quarto if we had HTML, LaTeX and Typst support.

@cscheid cscheid modified the milestones: Future, v1.7 Dec 11, 2024
@gordonwoodhull
Copy link
Contributor

gordonwoodhull commented Dec 11, 2024

That’s a good idea.

They mentioned on the community call that Typst is adding built-in support for wrapping text around images soon.

So if we add a dependency we should also plan to remove it soon.

@cscheid
Copy link
Collaborator

cscheid commented Dec 11, 2024

Typst is adding built-in support for wrapping text around images soon.

Did you get a sense of what "soon" meant? If it's 0.13-ish, then we probably want to wait for them. If not, then we should do what you suggested instead.

@gordonwoodhull
Copy link
Contributor

They were using words that sounded like “next release” but I wouldn’t want to bank on it because I know how software works.

I think it involves some more ambitious layouts than just this feature.

@multimeric
Copy link

I've been working on solving this problem with a Quarto plugin: multimeric/fwrappe. Currently only HTML and PDF/LaTeX are supported but I would welcome a typst implementation.

I do ultimately think this logic should live in quarto core because it's so fundamental, so I'm happy to contribute my code and be involved in discussions relating to that. I've written some technical docs that might be helpful for such an implementation. It's quite a tricky problem in LaTeX that won't have an easy solution. Mine is just scratching the surface.

@cscheid
Copy link
Collaborator

cscheid commented Jan 2, 2025

@multimeric I agree, and we'd love to take your contribution! Do you want to start a PR? How can we help?

@multimeric
Copy link

I assume this doesn't make sense as a Quarto feature until all the output formats are supported (correct me if this is not true), which means it can't be done incrementally. This is why I did it as an extension. On the other hand, extensions aren't really "powerful" enough to do this feature well, because they don't have access to all the lifecycle hooks (see #11749 and #11750), and also they don't have access to the much more extensive internal lua API. So I'm not sure the best way to proceed.

@cscheid
Copy link
Collaborator

cscheid commented Jan 3, 2025

I assume this doesn't make sense as a Quarto feature until all the output formats are supported (correct me if this is not true), which means it can't be done incrementally.

We have a number of features that don't span across all formats. We prefer many- or all-format features, but formats are different from each other for good reasons, and so we don't hold this too strongly. In addition, the rendering of a wrapfigure can degrade gracefully into a regular figure + warning.

I think HTML + LaTeX is a good minimal version that I'd be happy to merge. If we supported Typst that would be even nicer; there's some public-domain code to start from.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request latex LaTeX engines related libraries and technologies
Projects
None yet
Development

No branches or pull requests