1
votes

LaTeX files in two-column layout can make full width figures using a figure* environment instead of the standard figure. I'm generating my LaTeX via RMarkdown/pandoc, and so not sure if/how I can control the figure environment in this way. Is there any simple way to do this? Maybe by just overriding the figure environment definition?

For example, install the rticles R package and go -> new rmarkdown -> from template -> PNAS template (a two column template).

That provides a default example that renders a 1-column-width figure from markdown:


![Placeholder image of a frog with a long example caption to show
justification setting.<span data-label="fig:frog"></span>](frog.png)

knitting the .Rmd template, you will see this creates a .tex file that uses \begin{figure}. What change could you make to the template example to force pandoc to place the figure in a \begin{figure*} environment instead (short of a manual find-replace on the tex file?)

1
You could insert the \begin{figure*}...\end{figure*} as latex snipped - samcarter_is_at_topanswers.xyz
apologies for not providing a full MRE here. I should have said figures are produced by R code chunks, which are then rendered by pandoc. Pandoc turns markdown ![]() figure format into \begin{figure} on the fly - cboettig

1 Answers

1
votes

Here's a pandoc Lua filter which I'm using for a similar purpose. It basically reproduces pandoc's figure creation mechanism, but can be changed freely.

local function inline_latex(s)
  return pandoc.RawInline('latex', s)
end

local function to_label(s)
  return string.format(
    '\\label{%s}\n',
    s:gsub('([^%a%d%_%-%+%=%:%;%.])',
      function (x)
        io.stderr:write 'WARNING: Unicode char in label might not work!'
        return x
      end
    )
  )
end

--- Create custom code for figures
function Para (p)
  -- A paragraph is a figure if it contains an image and nothing else.
  local img = p.content[1]
  if img and img.t == 'Image' and #p.content == 1 then
    local linebreak = inline_latex '\n'
    local label = img.identifier ~= "" and
      inline_latex(to_label(img.identifier)) or
      inline_latex ""
    return pandoc.Para{
      inline_latex('\\begin{figure*}'),
      img, linebreak,
      inline_latex('\\caption'), pandoc.Span(img.caption), linebreak,
      label,
      inline_latex('\\end{figure*}')
    }
  end
end

The limitations of this approach is that Unicode characters in the figure label are no longer possible -- this could be fixed though by adjusting the to_label function.

See this great Bookdown manual section for more info abuut Lua filters and how to use them with R Markdown.