Skip to content
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

Enable latex_macros for output writers #9677

Closed
asmaier opened this issue Apr 21, 2024 · 6 comments
Closed

Enable latex_macros for output writers #9677

asmaier opened this issue Apr 21, 2024 · 6 comments

Comments

@asmaier
Copy link

asmaier commented Apr 21, 2024

At the moment the extension latex_macros can only be activated for input readers (https://pandoc.org/MANUAL.html#latex-macros). As far as I can see it is active by default e.g. for markdown and latex. However when converting the following test.tex

\documentclass[12pt,a4paper,twoside]{book}
\usepackage[intlimits]{amsmath}	

% brackets
\newcommand{\lra}[1]{ \left( #1 \right) }
\newcommand{\lrb}[1]{ \left[ #1 \right] }
\newcommand{\lrc}[1]{ \left\{ #1 \right\} }

\begin{document}

Lorem ipsum dolor sit amet.

\begin{align}
a & = \lra{b + c} \label{eq2} \\
c & = \lrb{d + e} \label{eq3} \\
e & = \lrc{f + g} \label{eq4}
\end{align}

Refer to \eqref{eq2} - \eqref{eq4}.

\end{document}

from latex to markdown test.md via

pandoc --wrap=preserve -s -t markdown -f latex -o test.md test.tex

the latex macros get expanded although that would not be necessary (because latex_macros extension is active for markdown by default):

Lorem ipsum dolor sit amet.

$$\begin{aligned}
a & =  \left( b + c \right)  \label{eq2} \\
c & =  \left[ d + e \right]  \label{eq3} \\
e & =  \left\{ f + g \right\}  \label{eq4}
\end{aligned}$$

Refer to [\[eq2\]](#eq2){reference-type="eqref" reference="eq2"} - [\[eq4\]](#eq4){reference-type="eqref" reference="eq4"}.

I figured out, that one can prevent the expansion of the latex macros by using the input format latex-latex_macros like

pandoc --wrap=preserve -s -t markdown -f latex-latex_macros -o test.md test.tex

which results in

    ```{=latex}
    \newcommand{\lra}[1]{ \left( #1 \right) }
    ```
    ```{=latex}
    \newcommand{\lrb}[1]{ \left[ #1 \right] }
    ```
    ```{=latex}
    \newcommand{\lrc}[1]{ \left\{ #1 \right\} }
    ```
    Lorem ipsum dolor sit amet.
    
    $$\begin{aligned}
    a & = \lra{b + c} \label{eq2} \\
    c & = \lrb{d + e} \label{eq3} \\
    e & = \lrc{f + g} \label{eq4}
    \end{aligned}$$
    
    Refer to [\[eq2\]](#eq2){reference-type="eqref" reference="eq2"} - [\[eq4\]](#eq4){reference-type="eqref" reference="eq4"}.

However this won't render correctly.

So my suggestion would be to not extend latex macros if the output format has explicitily activated the extension latex_macros, e.g.

pandoc -t markdown+latex_macros -f latex-o test.md test.tex

This could also be done for HTML output like

pandoc -s --mathjax -t html5+latex_macros -f markdown -o test.html test.md

because mathjax supports latex macros, too : https://docs.mathjax.org/en/latest/input/tex/macros.html .

@jgm
Copy link
Owner

jgm commented Apr 21, 2024

However this won't render correctly.

Can you expand on this? How are you rendering this md document?

So my suggestion would be to not extend latex macros if the output format has explicitily activated the extension latex_macros

The expansion takes place in the reader. By the time the writer gets it, it has already been expanded and there is nothing the writer can do to "unexpand" it. So, this needs to be a reader extension.

@asmaier
Copy link
Author

asmaier commented Apr 21, 2024

However this won't render correctly.

Can you expand on this? How are you rendering this md document?

I was using

 pandoc --mathjax -s -f markdown -t html5 -o test.html test.md

But this will not work correctly, because the \newcommands will be ignored.

So my suggestion would be to not extend latex macros if the output format has explicitily activated the extension latex_macros

The expansion takes place in the reader. By the time the writer gets it, it has already been expanded and there is nothing the writer can do to "unexpand" it. So, this needs to be a reader extension.

Ok, I understand. I was more thinking about a "readable" command line, ignoring the inner workings of pandoc. I still think it would be nice if the result in the markdown file would look like

    \newcommand{\lra}[1]{ \left( #1 \right) }
    \newcommand{\lrb}[1]{ \left[ #1 \right] }
    \newcommand{\lrc}[1]{ \left\{ #1 \right\} }

instead of

    ```{=latex}
    \newcommand{\lra}[1]{ \left( #1 \right) }
    ```
    ```{=latex}
    \newcommand{\lrb}[1]{ \left[ #1 \right] }
    ```
    ```{=latex}
    \newcommand{\lrc}[1]{ \left\{ #1 \right\} }
    ```

so that the latex macros won't get ignored when rendering the markdown.

@jgm
Copy link
Owner

jgm commented Apr 22, 2024

so that the latex macros won't get ignored when rendering the markdown.

Right. I guess that's something I hadn't considered in telling the Markdown writer to use the raw attribute syntax. We could (a) in the markdown writer, avoid using the raw attribute syntax, at least for macro definitions, or (b) in the markdown reader, process macros even when in raw blocks.

@asmaier
Copy link
Author

asmaier commented Apr 23, 2024

From a user perspective I would choose option (a), because it leads to a simpler markdown file.

@jgm
Copy link
Owner

jgm commented Apr 23, 2024

Note that you can always turn off the raw attribute extension:
-t markdown-raw_attribute.

jgm added a commit that referenced this issue Apr 23, 2024
blocks, unless there is no other option.

Mainly this is motivated by #9677.  If we put raw tex in a
raw_attribute block, then the macros don't get interpreted
when it is read again by pandoc's markdown reader.
@jgm
Copy link
Owner

jgm commented Apr 23, 2024

I've changed the writer so it doesn't use raw_attribute syntax unless there is no other option.

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

No branches or pull requests

2 participants