quarto-dev / quarto-cli

Open-source scientific and technical publishing system built on Pandoc.
https://quarto.org
Other
3.94k stars 325 forks source link

PDF has bad display of code-line-numbers combined with code-block-border-left #10798

Open lvxvvv opened 1 month ago

lvxvvv commented 1 month ago

Bug description

Description

When code-line-numbers is true and code-block-border-left is some color, the numbers overlay and overflow the border, resulting in something like this:

quarto-pdf-ln-bl

Steps to reproduce

File: mwe.qmd

---
title: "MWE"
format:
  pdf:
    code-line-numbers: true
    code-block-bg: false
    code-block-border-left: "#F0F0F0"
---

```{.python}
print("This is a MWE.")

while True:
    print("code-line-numbers and code-block-border-left mwe.")


Then run:

`quarto render mwe.qmd --to pdf`

### Expected behavior

The numbers should not overlay the left border. Instead, they should be completely on its left, with some margin (something like what happens with the epub output). Or, alternatively, the numbers could overlay the border, but not overflow it. That is, the border width must large enough to accommodate the numbers within its limits.

### Actual behavior

The numbers overlay and overflow the left border.

### Your environment

- OS: Debian 12.7

### Quarto check output

Quarto 1.5.57
[✓] Checking versions of quarto binary dependencies...
      Pandoc version 3.2.0: OK
      Dart Sass version 1.70.0: OK
      Deno version 1.41.0: OK
      Typst version 0.11.0: OK
[✓] Checking versions of quarto dependencies......OK
[✓] Checking Quarto installation......OK
      Version: 1.5.57
      Path: /opt/quarto/bin

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

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

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

[✓] Checking Python 3 installation....OK
      Version: 3.11.2
      Path: /usr/bin/python3
      Jupyter: 4.12.0
      Kernels: python3

(-) Checking Jupyter engine render....[IPKernelApp] ERROR | No such comm target registered: quarto_kernel_setup
[✓] Checking Jupyter engine render....OK

[✓] Checking R installation...........OK
      Version: 4.2.2
      Path: /usr/lib/R
      LibPaths:
        - /home/lux/R/x86_64-pc-linux-gnu-library/4.2
        - /usr/local/lib/R/site-library
        - /usr/lib/R/site-library
        - /usr/lib/R/library
      knitr: 1.48
      rmarkdown: 2.28

[✓] Checking Knitr engine render......OK
cderv commented 1 month ago

Some notes for us when dealing with this.

I wanted to understand how are this would be to tweak, so I had a quick look.

Here are some notes


For the context on this, we are using tcolorbox package to add this border.

Currently we do https://github.com/quarto-dev/quarto-cli/blob/fa772d3d8e7681f9be0c982577fb2bd263e12b85/src/resources/filters/layout/meta.lua#L92-L94

With 0pt for the offset. This means the the border is at the same place as the code line numbered added by Pandoc feature.

Full Tex code is

\begin{Shaded}
\begin{Highlighting}[numbers=left,,]
\BuiltInTok{print}\NormalTok{(}\StringTok{"This is a MWE."}\NormalTok{)}

\ControlFlowTok{while} \VariableTok{True}\NormalTok{:}
    \BuiltInTok{print}\NormalTok{(}\StringTok{"code{-}line{-}numbers and code{-}block{-}border{-}left mwe."}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

With

\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\usepackage{framed}
\definecolor{shadecolor}{RGB}{241,243,245}
\newenvironment{Shaded}{\begin{snugshade}}{\end{snugshade}}

(...)

\ifdefined\Shaded\renewenvironment{Shaded}{\begin{tcolorbox}[borderline west={3pt}{0pt}{shadecolor}, breakable, frame hidden, sharp corners, interior hidden, boxrule=0pt, enhanced]}{\end{tcolorbox}}\fi

If we use borderline west={3pt}{-9pt}{shadecolor} for example, this would avoid the problem (but also move left the boder in all case)

image

Not so visible when no background image

But will be different when one image

compare to now image

At the end this is a styling decision.

Though code-line-numbers feature will conflict with any tcolorbox customization that we are doing. No border here, but background changed image

We could also customize how the numbers are shown but we don't control that line for now. fancyvrb package is used and numbers=left config is written by Pandoc LaTeX writer directly https://github.com/jgm/pandoc/blob/56854c22f49df806625c5971e82d2e1ccc6f7127/src/Text/Pandoc/Writers/LaTeX.hs#L466-L468

We don't control this, so tweaking the option for this would require some LaTeX postprocessing.

Example of customization

Not that robust, but we already do some line postprocessing to tweak some content. So maybe it is not that hard to catch \begin{Highlighting}[numbers=left,] and replace by \begin{Highlighting}[numbers=left,numbersep=6pt,] when tcolorbox is added and used by Quarto.

Again, styling decision. Then we can do the necessary tweak.

Hope it helps