Discussion:
How to add LaTeX appendix support?
Christopher Charabaruk
2018-03-27 17:24:54 UTC
Permalink
What would it take to mark chapters with class `appendix` as appendices
instead, and have them appear properly in the TOC? Right now I'm
hand-editing LaTeX output from Pandoc to use the appendix package as well
as wrapping `\begin{appendices}` and `\end{appendices}` around the group of
chapters that I have added the class to in my source Markdown. Is this
something that could be done by filter or would it need changes to the
writer instead?
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+***@googlegroups.com.
To post to this group, send email to pandoc-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/0ede934e-8ae9-4f10-b79a-925ec4b03c8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
John MacFarlane
2018-03-27 22:43:25 UTC
Permalink
Definitely doable with a lua (or other) filter.

You just need to match on level-1 headers with class "appendix",
and change these into RawBlock "latex" with the appropriate
commands.
Post by Christopher Charabaruk
What would it take to mark chapters with class `appendix` as appendices
instead, and have them appear properly in the TOC? Right now I'm
hand-editing LaTeX output from Pandoc to use the appendix package as well
as wrapping `\begin{appendices}` and `\end{appendices}` around the group of
chapters that I have added the class to in my source Markdown. Is this
something that could be done by filter or would it need changes to the
writer instead?
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/0ede934e-8ae9-4f10-b79a-925ec4b03c8e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+***@googlegroups.com.
To post to this group, send email to pandoc-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/yh480k370lw35e.fsf%40johnmacfarlane.net.
For more options, visit https://groups.google.com/d/optout.
Chris Charabaruk
2018-03-27 22:59:34 UTC
Permalink
That doesn't sound like I can wrap all the sections defined by those
headers in an `appendices` environment, though. It sounds like I'd only be
able to modify the individual headers themselves.
Post by John MacFarlane
Definitely doable with a lua (or other) filter.
You just need to match on level-1 headers with class "appendix",
and change these into RawBlock "latex" with the appropriate
commands.
Post by Christopher Charabaruk
What would it take to mark chapters with class `appendix` as appendices
instead, and have them appear properly in the TOC? Right now I'm
hand-editing LaTeX output from Pandoc to use the appendix package as
well
Post by Christopher Charabaruk
as wrapping `\begin{appendices}` and `\end{appendices}` around the group
of
Post by Christopher Charabaruk
chapters that I have added the class to in my source Markdown. Is this
something that could be done by filter or would it need changes to the
writer instead?
--
You received this message because you are subscribed to the Google
Groups "pandoc-discuss" group.
Post by Christopher Charabaruk
To unsubscribe from this group and stop receiving emails from it, send
<javascript:>.
Post by Christopher Charabaruk
To view this discussion on the web visit
https://groups.google.com/d/msgid/pandoc-discuss/0ede934e-8ae9-4f10-b79a-925ec4b03c8e%40googlegroups.com.
Post by Christopher Charabaruk
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+***@googlegroups.com.
To post to this group, send email to pandoc-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/99cfc997-1243-482b-8f5f-6fd59c10db4f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
John MacFarlane
2018-03-28 00:25:43 UTC
Permalink
Post by Chris Charabaruk
That doesn't sound like I can wrap all the sections defined by those
headers in an `appendices` environment, though. It sounds like I'd only be
able to modify the individual headers themselves.
You can do virtually anything in a filter. However, I thought
of a simpler solution for you, since pandoc allows you to include
raw latex in markdown:

```
---
header-includes: '\usepackage{appendix}'
documentclass: book
...

# Chapter

ok

\appendices

# Appendix A

ok

# Appendix B

ok


\endappendices
```
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+***@googlegroups.com.
To post to this group, send email to pandoc-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/yh480kbmf911x4.fsf%40johnmacfarlane.net.
For more options, visit https://groups.google.com/d/optout.
Chris Charabaruk
2018-03-28 00:36:23 UTC
Permalink
I ended up figuring out a filter approach that at least wraps my appendices
in the environment. The only thing I don't have is adding the package, but
from your example there I think I might be able to figure that out too.

What I've got so far:

```lua
-- Wraps appendices in a `appendices` environment
in_appendices = false
function Header(el)
if el.level ~= 1 then return el end
if el.classes:includes("appendix") then
if not in_appendices then
in_appendices = true
return {pandoc.RawBlock("latex", "\\begin{appendices}"), el}
else
return el
end
elseif in_appendices then
in_appendices = false
return {pandoc.RawBlock("latex", "\\end{appendices}"), el}
else
return el
end
end
```
Post by John MacFarlane
Post by Chris Charabaruk
That doesn't sound like I can wrap all the sections defined by those
headers in an `appendices` environment, though. It sounds like I'd only
be
Post by Chris Charabaruk
able to modify the individual headers themselves.
You can do virtually anything in a filter. However, I thought
of a simpler solution for you, since pandoc allows you to include
```
---
header-includes: '\usepackage{appendix}'
documentclass: book
...
# Chapter
ok
\appendices
# Appendix A
ok
# Appendix B
ok
\endappendices
```
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+***@googlegroups.com.
To post to this group, send email to pandoc-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/1bc30c0d-ce6e-4ce0-8d28-f78599f384ae%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Chris Charabaruk
2018-03-28 02:16:20 UTC
Permalink
Yep, got it figured out. It was a bit tricky and I had to put together some
helper functions to figure out /what/ to add to the metadata, but I now
have a filter to handle appendices in LaTeX.

Now I wonder if it's possible to change <chapter>s in Docbook to
<appendix>s based on class, but that I'll save to another day...

```lua
function Meta(meta)
local packageBlock = pandoc.RawBlock("latex",
"\\usepackage[toc]{appendix}")
local headerIncludes = meta["header-includes"]
if headerIncludes then
print("Meta: found header-includes, adding usepackage for appendix")
table.insert(headerIncludes, packageBlock)
else
print("Meta: couldn't find header-includes, adding it in!")
headerIncludes = pandoc.MetaBlocks({packageBlock})
meta["header-includes"] = headerIncludes
end
return meta
end
```
Post by Chris Charabaruk
I ended up figuring out a filter approach that at least wraps my
appendices in the environment. The only thing I don't have is adding the
package, but from your example there I think I might be able to figure that
out too.
```lua
-- Wraps appendices in a `appendices` environment
in_appendices = false
function Header(el)
if el.level ~= 1 then return el end
if el.classes:includes("appendix") then
if not in_appendices then
in_appendices = true
return {pandoc.RawBlock("latex", "\\begin{appendices}"), el}
else
return el
end
elseif in_appendices then
in_appendices = false
return {pandoc.RawBlock("latex", "\\end{appendices}"), el}
else
return el
end
end
```
Post by John MacFarlane
Post by Chris Charabaruk
That doesn't sound like I can wrap all the sections defined by those
headers in an `appendices` environment, though. It sounds like I'd only
be
Post by Chris Charabaruk
able to modify the individual headers themselves.
You can do virtually anything in a filter. However, I thought
of a simpler solution for you, since pandoc allows you to include
```
---
header-includes: '\usepackage{appendix}'
documentclass: book
...
# Chapter
ok
\appendices
# Appendix A
ok
# Appendix B
ok
\endappendices
```
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+***@googlegroups.com.
To post to this group, send email to pandoc-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/6f173aa4-a4f6-4457-92d7-b03eeabeb4ee%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Christophe Demko
2018-04-01 19:45:50 UTC
Permalink
You can have a look
to https://github.com/chdemko/pandoc-latex-color/blob/master/pandoc_latex_color.py#L229
to add LaTeX headers
Post by Chris Charabaruk
Yep, got it figured out. It was a bit tricky and I had to put together
some helper functions to figure out /what/ to add to the metadata, but I
now have a filter to handle appendices in LaTeX.
Now I wonder if it's possible to change <chapter>s in Docbook to
<appendix>s based on class, but that I'll save to another day...
```lua
function Meta(meta)
local packageBlock = pandoc.RawBlock("latex",
"\\usepackage[toc]{appendix}")
local headerIncludes = meta["header-includes"]
if headerIncludes then
print("Meta: found header-includes, adding usepackage for appendix")
table.insert(headerIncludes, packageBlock)
else
print("Meta: couldn't find header-includes, adding it in!")
headerIncludes = pandoc.MetaBlocks({packageBlock})
meta["header-includes"] = headerIncludes
end
return meta
end
```
Post by Chris Charabaruk
I ended up figuring out a filter approach that at least wraps my
appendices in the environment. The only thing I don't have is adding the
package, but from your example there I think I might be able to figure that
out too.
```lua
-- Wraps appendices in a `appendices` environment
in_appendices = false
function Header(el)
if el.level ~= 1 then return el end
if el.classes:includes("appendix") then
if not in_appendices then
in_appendices = true
return {pandoc.RawBlock("latex", "\\begin{appendices}"), el}
else
return el
end
elseif in_appendices then
in_appendices = false
return {pandoc.RawBlock("latex", "\\end{appendices}"), el}
else
return el
end
end
```
Post by Chris Charabaruk
Post by Chris Charabaruk
That doesn't sound like I can wrap all the sections defined by those
headers in an `appendices` environment, though. It sounds like I'd
only be
Post by Chris Charabaruk
able to modify the individual headers themselves.
You can do virtually anything in a filter. However, I thought
of a simpler solution for you, since pandoc allows you to include
```
---
header-includes: '\usepackage{appendix}'
documentclass: book
...
# Chapter
ok
\appendices
# Appendix A
ok
# Appendix B
ok
\endappendices
```
--
You received this message because you are subscribed to the Google Groups "pandoc-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pandoc-discuss+***@googlegroups.com.
To post to this group, send email to pandoc-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pandoc-discuss/0e3a3a8e-5cdc-4ce0-8ec8-4f3a22bb4772%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...