Skip to content

PDF generation from Markdown fails when a code block is written into an Alert #10641

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

Open
Cudochi opened this issue Feb 24, 2025 · 7 comments
Open
Labels

Comments

@Cudochi
Copy link

Cudochi commented Feb 24, 2025

Pandoc 3.6.3 on Ubuntu 24.04.01 LTS

Doing this kind of Markdown (code block inside an alert) makes the generation fail :

> [!IMPORTANT]
> ```xml
> <test name="placeholder">S</test> 
> ```
> This is a text with **Markdown** _formating_.

The error message :

pandoc --pdf-engine=xelatex -f gfm+raw_attribute "working_dir/test.md" -o test.pdf --template="tex_files/template_no_Cover_no_TOC.tex" --highlight-style=breezeDark -V author="XXX" -V title="XXX" -V version="XXX" -V subtitle="XXX" -V date="24/02/2025"
Error: pandoc conversion failed.
Error producing PDF.
! LaTeX Error: \begin{tcb@savebox} on input line 573 ended by \end{document}.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
...
l.764 \end{document}
Script failed with exit code 2 

Are we supposed to be able to do that, or is it me who is trying to do something that is not supposed to happen ?

With regards

@Cudochi Cudochi added the bug label Feb 24, 2025
@jgm
Copy link
Owner

jgm commented Feb 24, 2025

This works fine for me without the custom template, so the issue has to do with the custom template you're using.

Do you run into the problem with just the code block (without the alert)? If so, then I suspect your custom template is missing things needed for highlighting. If not, then I'm not sure, but it's impossible to say without seeing the template.

@jgm
Copy link
Owner

jgm commented Feb 24, 2025

Before reporting back here, please do what you can to debug this on your own. Test with an alert without a code block. Compare your custom template to the default template to see what might be missing. And see if the person who wrote your custom template (if it wasn't you) can help.

@Cudochi
Copy link
Author

Cudochi commented Feb 24, 2025

I can write both code blocks and alerts in my document with no problem, the crash specifically happens when I put a code block inside an alert.

When you talk about default template, do you mean the template used by Pandoc when no template is provided, or do you refer to the templates in this repository ?
If it is the second one, I couldn't find any documentation/procedure on how to use them (the template for latex seems to be split across multiple files). Could you point me to some, please ?

I will continue to debug this on my own, thank you for your time.

@jgm
Copy link
Owner

jgm commented Feb 24, 2025

When you talk about default template, do you mean the template used by Pandoc when no template is provided, or do you refer to the templates in this repository ?

These should be the same (unless you have overridden the default template in your user data directroy). You can get the default template using pandoc -D latex. Yes, it uses partials. These are described in the templates section of the manual. I would advise starting with the default template (which will use the default partials) and trying to locate your changes there; in most cases you shouldn't need to mess with the partials.

@Cudochi
Copy link
Author

Cudochi commented Feb 25, 2025

I think I found the problem : There's nothing wrong with Pandoc as far as I can tell, nor with the template.
I haven't created this system, but I found out that the markdown file is modified by a python program. It's not a filter, but a program that modify the .md file before it's feed into Pandoc.
This program converts this :

> [!IMPORTANT]
> ```xml
> <test name="placeholder">S</test>
> <test name="placeholder">S</test> 
> ```
> This is a text with **Markdown** _formating_.

Into this (with the gfm+raw_attribute tag in Pandoc's command) :

```{=latex}
\begin{importantbox}
```
xml
<test name="placeholder">S</test>
<test name="placeholder">S</test> 
```
This is a text with **Markdown** _formating_.
\end{importantbox}
```

importantbox is then customized in the template like this :

%--------------------------------------------
% IMPORTANT
%--------------------------------------------
\newtcolorbox{importantbox}[1][]{
  enhanced,
  sharp corners,
  borderline west={2pt}{0pt}{indigoCOMPANY1!70!black},
  colback=indigoCOMPANY1!5!white,
  colframe=indigoCOMPANY1!75!black,
  fonttitle=\bfseries,
  coltitle=white,
  colbacktitle=indigoCOMPANY1!75!black,
  title=\faComment\ Note Importante,
  #1
}

The problem is that the ``` of the two blocks are conflicting with each other, but even before that, I'm fairly certain this isn't how one is supposed to handle alerts. The bold and italic text under the code block in the note is not interpreted either because it is no longer markdown.
I've inherited the charge of maintaining this system from an employee who was here before me, but I struggle to find an example, or documentation on how the alerts extension should be used (because if I understand what I see correctly, the extension is not even used with this setup). Could you point me to it, please ?

@jgm
Copy link
Owner

jgm commented Feb 25, 2025

You can see how pandoc parses this by doing:

% pandoc -t native -f gfm+raw_attribute
```{=latex}
\begin{importantbox}
```
xml
<test name="placeholder">S</test>
<test name="placeholder">S</test> 
```
This is a text with **Markdown** _formating_.
\end{importantbox}
```
[ RawBlock (Format "latex") "\\begin{importantbox}\n"
, Para
    [ Str "xml"
    , SoftBreak
    , RawInline (Format "html") "<test name=\"placeholder\">"
    , Str "S"
    , RawInline (Format "html") "</test>"
    , SoftBreak
    , RawInline (Format "html") "<test name=\"placeholder\">"
    , Str "S"
    , RawInline (Format "html") "</test>"
    ]
, CodeBlock
    ( "" , [] , [] )
    "This is a text with **Markdown** _formating_.\n\\end{importantbox}"
]

Clearly this isn't what was desired. If you modify the python program to produce

```{=latex}
\begin{importantbox}
```

```xml
<test name="placeholder">S</test>
<test name="placeholder">S</test> 
```

This is a text with **Markdown** _formating_.

```{=latex}
\end{importantbox}
```

then you might be getting closer. But a better solution would be to use a Lua filter (see #9821 ).

@jgm
Copy link
Owner

jgm commented Feb 25, 2025

The filter in #9821 is actually just for PDF via typst, but you could modify it easily enough for PDF via LaTeX.
Basically you'd want to intercept a Div with class important and render its contents between two latex RawBlocks with \begin{importantbox} and \end{importantbox}.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants