Pandoc filter for embedding file in Latex output #9524
-
ContextI use Obsidian for taking notes and often I embed files directly in the note. Including files in Obsidian works the same way as embeding an image.
I made a template to convert notes to PDF. I would like to embed files in the generated output as this could be very handy for sharing document mentioned during a team meetup or to share document linked to the content of the note. SolutionIt is possible to embed files in PDF output thanks to the I would like to make a filter to make the following transform (pseudocode) :
I seems really simple but I struggle a lot with the LUA syntax! ^^' |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Here is a python filter which does that : from pandocfilters import toJSONFilter, RawInline, Str, Image
import re
from pathlib import Path
def latex(x):
return RawInline('latex', x)
def latexembed(key, value, format, meta):
if format == "latex":
if key == 'Image':
placeholder_image = Path(__file__).parent / 'paperclip.png'
style, caption, file = value
path = file[0]
if headers := meta.get("header-includes"):
if absolute_path_regex := re.search(r"graphicspath\{\{([^\}]+)\}\}", str(headers)):
path = absolute_path_regex.group(1) + path
if re.search(r"\.(csv|xlsx|pdf|py|zip|md|txt|docx)$", path, flags=re.IGNORECASE):
replacement = "\\includegraphics[height=0.5cm]{"+ str(placeholder_image.as_posix()) + "} "
return ([latex(replacement), Str(Path(path).name), latex("\\embedfile{" + path + "}")])
if __name__ == "__main__":
toJSONFilter(latexembed) This solution need a file saved with the name This way attachments are displayed in the PDF Viewer, for exemple with Adobe Acrobat Reader : |
Beta Was this translation helpful? Give feedback.
Here is a python filter which does that :