tomduck / pandoc-xnos

Library code for pandoc-fignos/eqnos/tablenos/secnos.
GNU General Public License v3.0
106 stars 27 forks source link

Try fixing a bug that sometimes "cleveref_fakery" was inserted into wrong position #7

Closed gepcel closed 5 years ago

gepcel commented 6 years ago

When working with another filter pandoc-latex-admonition, if filter pandoc-fignos is applied after pandoc-latex-admonition, the "cleveref_fakery" raw tex is inserted into a wrong position.

See an example:

---
pandoc-latex-admonition:
    - color: dodgerblue
      classes: [con]
      linewidth: 3
      margin: 12
      innermargin: 24
fignos-cleveref: True
---

```con
python

Run `pandoc -o test.tex --filter=pandoc-latex-admonition --filter=pandoc-fignos file.md'. Get a wrong result, which cann't be converted to pdf:

**The wrong tex**:
```tex
\begin{env-1b47dc9f-88e2-4639-809e-2e9091ef6b4a}

% pandoc-xnos: cleveref fakery
\newcommand{\plusnamesingular}{}
\newcommand{\starnamesingular}{}
\newcommand{\xrefname}[1]{\protect\renewcommand{\plusnamesingular}{#1}}
\newcommand{\Xrefname}[1]{\protect\renewcommand{\starnamesingular}{#1}}
\providecommand{\cref}{\plusnamesingular~\ref}
\providecommand{\Cref}{\starnamesingular~\ref}
\providecommand{\crefformat}[2]{}
\providecommand{\Crefformat}[2]{}

% pandoc-xnos: cleveref formatting
\crefformat{figure}{fig.~#2#1#3}
\Crefformat{figure}{Figure~#2#1#3}

\begin{verbatim}
python
\end{verbatim}

\end{env-1b47dc9f-88e2-4639-809e-2e9091ef6b4a}

Which \begin{env-*************} should be right before \begin{verbatim}.

After some testing, this commit fixed this specific issue. But I'm not sure I understand the philosophy of this filter, so tell me what to do if I'm wrong.

tomduck commented 6 years ago

Thanks for your feedback. I tested your example, and find it compiles if I use the --standalone flag with pandoc. I'm not sure that the problem you are running into is due to the cleveref fakery of pandoc-xnos. It seems to me that you need a standalone TeX document.

The philosophy of the pandoc-fignos filter (and friends) is to allow cross-referencing with as little fuss as possible for pdf/TeX and html, and other formats where possible. The cleveref fakery was introduced to mitigate users having to manually insert the cleveref package into their latex template. If you want to use the cleveref package than you can turn off cleveref fakery by setting xnos-cleveref-fake to False.

Note that the change that you proposed (and code is always appreciated!) did not work with the integration tests in the pandoc-fignos project.

Nevertheless, please let me know if you still think there is a bug. I'm happy to help hunt it down and squash it.

gepcel commented 6 years ago

Let me try explaining myself. I didn't use --standalone on purpose to get a clear result to make an example. The problem exists either way.

Let's say there's a test.md file, as following:

---
pandoc-latex-admonition:
    - color: dodgerblue
      classes: [con]
      linewidth: 3
      margin: 12
      innermargin: 24
fignos-cleveref: True
---

```con
python
python
python

Following by a logo @fig:logo

Python logo{#fig:logo}

The file contains a front-matter and two parts of contents: admonition part and fignos part. Then try converting the `test.md` file to pdf with two commands:

**Command 1** (get's the expected pdf, because `fignos` runs before `admonition`)

pandoc -o test_xnos_before.pdf --filter=pandoc-fignos --filter=pandoc-latex-admonition test.md


**Command 2** (get's an error, because `fignos` runs afterward)

pandoc -o test_xnos_after.pdf --filter=pandoc-latex-admonition --filter=pandoc-fignos test.md

The error would be:

Error producing PDF. ! Undefined control sequence. l.254 Following by a logo \xrefname



The reason (at least the reason I think) would be that: the admonition filter generates an `\begin{env-****} ... \end{env-****}` environment, and `xnos` filter should insert tex code outside this env not inside it.

I'm pretty sure about the problem, and this fixed this specific problem. However I'm not sure this fix is the right way, and it won't cause more problems.

I've encountered this for a long time, every time I'll have to remember to put all `pandoc-*nos` filter family before the `pandoc-latex-admonition` filter, which is really annoying.
tomduck commented 5 years ago

The cleveref fakery has been removed from the 2.0 branch of all of the filters. I think that a much more sane approach is now used. I am planning to do a full release quite soon. For now there are betas available.