Convert heading to latex command #6228
-
DescriptionHow to go from (.qmd):
to latex (.tex):
Do I need to write a lua filter? |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
I think a filter is your best bet. There is this filter which almost (but not quite, I think) does what you're looking for: https://github.com/quarto-ext/latex-environment Might be a nice addition to this extension - I'll try to add it soon! |
Beta Was this translation helpful? Give feedback.
-
I'm going to just write out the solution because this is a good "Hello, World" filter from which you can learn to generalize: -- filter.lua
-- this function will be executed for all Header elements in the document
Header = function(el)
-- el.level == 1 makes it so you only catch "# ..." and not "## ...", etc
-- el.classes is a pandoc List object, which has the method "includes"
if el.level == 1 and el.classes:includes("bmsection") then
-- pandoc.utils.stringify converts a pandoc value (in this case,
-- the list of inlines that make up the header) into a plain string.
local text = pandoc.utils.stringify(el.content)
-- a RawBlock element contains a string that is passed through
-- directly to the final element.
local result = pandoc.RawBlock("latex", "\\bmsection*{" .. text .. "}")
return result
else
-- a "return nil" statement is implied at the end of a lua function, but
-- if you want to be explicit, you can add it.
-- returning "nil" means "do not modify this element"
return nil
end
end For the following document, ---
title: test
filters:
- filter.lua
---
Some text.
# Acknowledgments {.bmsection}
Some acks. You get this output:
|
Beta Was this translation helpful? Give feedback.
-
Hey guys!, Thank you so much for your flash answers! @dragonstyle : Indeed, I am already using the latex-environment extension, it's quite useful! But yeah, using it for environments only. @cscheid : thank you so much for sharing the lua filter code! Valeu! P.S. the more I use Quarto, the more I understand I'll have to learn Pandoc & Lua... :) |
Beta Was this translation helpful? Give feedback.
-
@cscheid I am so happy I managed to adapt your code to handle unnumbered sections (probably not the most idiomatic Lua code, but works!): Header = function(el)
-- el.level == 1 makes it so you only catch "# ..." and not "## ...", etc
-- el.classes is a pandoc List object, which has the method "includes"
if el.level == 1 and el.classes:includes("bmsection") then
if el.classes:includes("unnumbered") then
-- pandoc.utils.stringify converts a pandoc value (in this case,
-- the list of inlines that make up the header) into a plain string.
local text = pandoc.utils.stringify(el.content)
-- a RawBlock element contains a string that is passed through
-- directly to the final element.
local result = pandoc.RawBlock("latex", "\\bmsection*{" .. text .. "}")
return result
else
-- pandoc.utils.stringify converts a pandoc value (in this case,
-- the list of inlines that make up the header) into a plain string.
local text = pandoc.utils.stringify(el.content)
-- a RawBlock element contains a string that is passed through
-- directly to the final element.
local result = pandoc.RawBlock("latex", "\\bmsection{" .. text .. "}")
return result
end
else
-- a "return nil" statement is implied at the end of a lua function, but
-- if you want to be explicit, you can add it.
-- returning "nil" means "do not modify this element"
return nil
end
end You don't happen to know how I could change it still to handle references, i.e. to take # Heading text {#ref .bmsection .unnumbered} and convert \bmsection*{heading text\label{ref}} |
Beta Was this translation helpful? Give feedback.
I'm going to just write out the solution because this is a good "Hello, World" filter from which you can learn to generalize: