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

error rendering to pdf when including Python error message with containing \ as e.g. np.testing.assert_equal #11695

Open
kandolfp opened this issue Dec 17, 2024 · 0 comments
Assignees
Labels
bug Something isn't working jupyter latex LaTeX engines related libraries and technologies
Milestone

Comments

@kandolfp
Copy link

Bug description

Important

In short the problem is that some Python error messages contain \ (backslash) that needs to be translated to \textbackslash{} for LaTeX output

Note

Follow up to #10891.

When rendering the following code chunk with #| error: true and the Python code that uses the numpy.testing.assert_equal.

The problem is with the error message itself

---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[2], line 6
      4 A = np.random.rand(3, 3)
      5 X = LA.inv(A)
----> 6 np.testing.assert_equal(A @ X, np.eye(A.shape[0]))

    [... skipping hidden 1 frame]

File .venv/lib/python3.12/site-packages/numpy/_utils/__init__.py:85, in _rename_parameter.<locals>.decorator.<locals>.wrapper(*args, **kwargs)
     83             raise TypeError(msg)
     84         kwargs[new_name] = kwargs.pop(old_name)
---> 85 return fun(*args, **kwargs)

    [... skipping hidden 1 frame]

File .venv/lib/python3.12/site-packages/numpy/testing/_private/utils.py:884, in assert_array_compare(comparison, x, y, err_msg, verbose, header, precision, equal_nan, equal_inf, strict, names)
    879         err_msg += '\n' + '\n'.join(remarks)
    880         msg = build_err_msg([ox, oy], err_msg,
    881                             verbose=verbose, header=header,
    882                             names=names,
    883                             precision=precision)
--> 884         raise AssertionError(msg)
    885 except ValueError:
    886     import traceback

AssertionError: 
Arrays are not equal

Mismatched elements: 7 / 9 (77.8%)
Max absolute difference among violations: 1.21927e-16
Max relative difference among violations: 1.11022302e-16
 ACTUAL: array([[ 1.000000e+00,  4.295956e-18,  1.526214e-17],
       [ 1.433446e-17,  1.000000e+00, -1.007895e-16],
       [ 4.090201e-17, -1.219270e-16,  1.000000e+00]])
 DESIRED: array([[1., 0., 0.],
       [0., 1., 0.],
       [0., 0., 1.]])

that contains two \n in the line 879 err_msg += '\n' + '\n'.join(remarks).
This gets translated to

\textcolor{QuartoInternalColor2}{}\textcolor{QuartoInternalColor4}{    884}\textcolor{QuartoInternalColor2}{         err_msg }\textcolor{QuartoInternalColor8}{+}\textcolor{QuartoInternalColor2}{}\textcolor{QuartoInternalColor8}{=}\textcolor{QuartoInternalColor2}{ }\textcolor{QuartoInternalColor6}{'}\textcolor{QuartoInternalColor2}{}\textcolor{QuartoInternalColor16}{\n}\textcolor{QuartoInternalColor2}{}\textcolor{QuartoInternalColor6}{'}\textcolor{QuartoInternalColor2}{ }\textcolor{QuartoInternalColor8}{+}\textcolor{QuartoInternalColor2}{ }\textcolor{QuartoInternalColor6}{'}\textcolor{QuartoInternalColor2}{}\textcolor{QuartoInternalColor16}{\n}\textcolor{QuartoInternalColor2}{}\textcolor{QuartoInternalColor6}{'}\textcolor{QuartoInternalColor2}{}\textcolor{QuartoInternalColor8}{.}\textcolor{QuartoInternalColor2}{join(remarks)}

and here we have a problem with the \n that can be fixed by replacing it with \textbackslash{}n.

Steps to reproduce

--- 
title: "foo"
---

```{python}
#| error: true
import numpy as np
from numpy import linalg as LA

A = np.random.rand(3, 3)
X = LA.inv(A)
np.testing.assert_equal(A @ X, np.eye(A.shape[0]))
```                 

Expected behavior

I run:

quarto render file.qmd --debug --to pdf

I expected a pdf to be produced, containing the Python error message and traceback.

Actual behavior

quarto render file.qmd --to pdf

Starting python3 kernel...Done

Executing 'file.quarto_ipynb'
  Cell 1/1: ''...Done

pandoc 
  to: latex
  output-file: file.tex
  standalone: true
  pdf-engine: xelatex
  variables:
    graphics: true
    tables: true
  default-image-extension: pdf
  
metadata
  documentclass: scrartcl
  classoption:
    - DIV=11
    - numbers=noendperiod
  papersize: letter
  header-includes:
    - \KOMAoption{captions}{tableheading}
  block-headings: true
  title: foo
  

Rendering PDF
running xelatex - 1
  This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023/Debian) (preloaded format=xelatex)
   restricted \write18 enabled.
  entering extended mode
  
updating tlmgr

updating existing packages
ERROR: 
compilation failed- error
Undefined control sequence.
<argument> \n 
              
l.252 ...olor{QuartoInternalColor2}{join(remarks)}
                                                   

see file.log for more information.

Your environment

  • Ubuntu 24.04
  • Quarto > 1.6.39 (tested also with latest build on github)

Quarto check output

quarto check
Quarto 1.7.3
[✓] Checking environment information...
      Quarto cache location: ~/.cache/quarto
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.4.0: OK
      Dart Sass version 1.70.0: OK
      Deno version 1.46.3: OK
      Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.7.3
      Path: /tmp/quartodebug/.venv/lib/python3.12/site-packages/quarto_cli/quarto-1.7.3/bin

[✓] Checking tools....................OK
      TinyTeX: (not installed)
      Chromium: (not installed)

[✓] Checking LaTeX....................OK
      Using: Installation From Path
      Path: /usr/bin
      Version: 2023

[✓] Checking basic markdown render....OK

[✓] Checking Python 3 installation....OK
      Version: 3.12.3
      Path: /tmp/quartodebug/.venv/bin/python3
      Jupyter: 5.7.2
      Kernels: python3

[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........(None)

      Unable to locate an installed version of R.
      Install R from https://cloud.r-project.org/
@kandolfp kandolfp added the bug Something isn't working label Dec 17, 2024
@cscheid cscheid added jupyter latex LaTeX engines related libraries and technologies labels Dec 17, 2024
@cscheid cscheid self-assigned this Dec 17, 2024
@cscheid cscheid added this to the v1.7 milestone Dec 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working jupyter latex LaTeX engines related libraries and technologies
Projects
None yet
Development

No branches or pull requests

2 participants