T-F-S / tcolorbox

A LaTeX package to create highly customizable colored boxes.
http://www.ctan.org/pkg/tcolorbox
LaTeX Project Public License v1.3c
234 stars 16 forks source link

Dimension too large #304

Open make-github-pseudonymous-again opened 2 days ago

make-github-pseudonymous-again commented 2 days ago

I am trying to generate a PDF with the listings of a bundled JavaScript package. I am getting the following error with minified JavaScript code, probably because the file is a single line of code (>15k characters):

! Dimension too large.
\lsthk@InitVarsEOL ->\ifdim \lst@currlwidth 
                                            >\lst@maxwidth \global \lst@maxw...
l.1 ...rminated:function(){}});return new l(j,p)};
\newtcbinputlisting[use counter=filePrg, number format=\arabic]{\codeFromFile}[3]{%
  listing engine=listings,
  minted language=#1,
  listing file={#2},
  minted options={%
    autogobble,%
    linenos,%
    breaklines,%
    breakanywhere%
  },
  listing only,
  size=title,
  arc=1.5mm,
  breakable,
  enhanced jigsaw,
  colframe=brown,
  coltitle=White,
  boxrule=0.5mm,
  colback=white,
  coltext=Black,
  title=%\TwoSymbolsAndText{\faCode}{%
    \textbf{File \thetcbcounter}\textbf{:} \textit{#3}%
  %}{\faCode}
}

This error occurs even when using the text lexer:

\codeFromFile{text}{file.js}{File}

Is there any way to make this work?

muzimuzhi commented 2 days ago

Could you make your example complete thus compilable? For example Load necessary packages, define needed counter, provide loaded external file.js, etc.

BTW you set to use listings engine, but the text lexer is only passed on to minted-specific option, thus is ignored. And the default setting listing options={style=tcblatex} would take effect.

  listing engine=listings,
  minted language=#1,
  listing file={#2},
  minted options={%
    autogobble,%
    linenos,%
    breaklines,%
    breakanywhere%
  },
make-github-pseudonymous-again commented 2 days ago

BTW you set to use listings engine, but the text lexer is only passed on to minted-specific option, thus is ignored. And the default setting listing options={style=tcblatex} would take effect.

My mistake. Tried multiple approaches.

Could you make your example complete thus compilable? For example Load necessary packages, define needed counter, provide loaded external file.js, etc.

Will do.

make-github-pseudonymous-again commented 2 days ago

@muzimuzhi Here you go

# Makefile
TARGET := listings
LTX := xelatex -shell-escape -interaction=nonstopmode -halt-on-error

$(TARGET).pdf: $(TARGET).tex package/dist/preact.js
    $(LTX) $(TARGET).tex

package/dist/preact.js:
    wget https://registry.npmjs.org/preact/-/preact-10.25.0.tgz
    tar -xzf preact-10.25.0.tgz
% listings.tex
\documentclass{article}

\newcounter{filePrg}

\usepackage{geometry}
\geometry{paper=letterpaper,margin=2cm}

\usepackage[dvipsnames]{xcolor}

\usepackage[many]{tcolorbox}
\tcbuselibrary{minted}

\newtcbinputlisting[use counter=filePrg, number format=\arabic]{\codeFromFile}[3]{%
  listing engine=minted,
  minted language=#1,
  listing file={#2},
  minted options={%
    autogobble,%
    linenos,%
    breaklines,%
    breakanywhere%
  },
  listing only,
  size=title,
  arc=1.5mm,
  breakable,
  enhanced jigsaw,
  colframe=brown,
  coltitle=White,
  boxrule=0.5mm,
  colback=white,
  coltext=Black,
  title=\textbf{File \thetcbcounter}\textbf{:} \textit{#3}%
}

\usepackage{cleveref}
\crefname{filePrg}{file program}{file programs}

\begin{document}
    \codeFromFile{text}{package/dist/preact.js}{package/dist/preact.js}
\end{document}
muzimuzhi commented 1 day ago

Good news: With listing engine=minted, your example compiles without errors.

Bad news: The output is still wrong. The line breaking mechanism provided by minted just didn't work.

Screenshot of unbroken looooong code line

![image](https://github.com/user-attachments/assets/46bcd6b2-768a-41da-be2c-51ba0f47a4ca)

This is reproducible with fvextra only (which is the source of breaklines and breakanywhere minted options), using

\documentclass{article}
\usepackage{fvextra}

\begin{document}
\VerbatimInput[linenos, breaklines, breakanywhere]{package/dist/preact.js}
\end{document}

PS: The initial error was thrown by listings internals (notice the lst prefix). But with appropriate number of characters, fvextra (thus minted) would throw the same error, which comes down to one of TeX's limitations.

! Dimension too large.
\lsthk@InitVarsEOL ->\ifdim \lst@currlwidth 
                                            >\lst@maxwidth \global \lst@maxw...
l.1 ...rminated:function(){}});return new l(j,p)};
muzimuzhi commented 1 day ago

I filed https://github.com/gpoore/fvextra/issues/28 and added some discussion there.

Even though the line breaking mechanism may be fixed for super long line (https://github.com/gpoore/fvextra/issues/28#issuecomment-2510129720), you still hit the limitation of fvextra that a broken line cannot across multiple pages, thus cannot be broken into sequences of tcolorboxes.

In short, currently there seems to be no direct support for typesetting super long line of code (arbitrary combination of caracters), with code highlighting and automatic line and page breaking. Maybe url package is helpful.