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
224 stars 16 forks source link

\tcbuselibrary{minted} doesn't work anymore #158

Closed dbitouze closed 2 years ago

dbitouze commented 2 years ago

The minted example, page 321 of the documentation of tcolorbox version 4.51, works smoothly with just minted:

\documentclass{article}
\usepackage{minted}
\begin{document}
\begin{minted}{xml}
<?xml version="1.0"?>
<project name="Package tcolorbox" default="documentation" basedir=".">
<description>
Apache Ant build file (http://ant.apache.org/)
</description>
</project>
\end{minted}
\end{document}

but not with \tcbuselibrary{minted}:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{minted}
\begin{document}
\begin{tcblisting}{colback=yellow!5,colframe=yellow!50!black,listing only,
title=This is source code in another language (XML), fonttitle=\bfseries,
listing engine=minted,minted language=xml}
<?xml version="1.0"?>
<project name="Package tcolorbox" default="documentation" basedir=".">
<description>
Apache Ant build file (http://ant.apache.org/)
</description>
</project>
\end{tcblisting}
\end{document}

the error being:

(./_minted-test/default-pyg-prefix.pygstyle) (./_minted-test/default.pygstyle)  File "<string>", line 1
    import sys; import os; import textwrap; from io import open; fname = os.path.expanduser(os.path.expandvars("test.listing")); f = open(fname, "r", encoding="UTF8") if os.path.isfile(fname) else None; t = f.readlines() if f is not None else None; t_opt = t if t is not None else []; f.close() if f is not None else None; tmpfname = os.path.expanduser(os.path.expandvars("test.pyg")); f = open(tmpfname, "w", encoding="UTF8") if t is not None else None; fvstartnum = 0; fvstopnum = \FancyVerbStopNum ; s = fvstartnum-1 if fvstartnum != 0 else 0; e = fvstopnum if fvstopnum != 0 else len(t_opt); [f.write(textwrap.dedent("".join(x))) for x in (t_opt[0:s], t_opt[s:e], t_opt[e:]) if x and t is not None]; f.close() if t is not None else os.remove(tmpfname);
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   ^
SyntaxError: unexpected character after line continuation character
system returned with code 256
Error: cannot read infile: [Errno 2] No such file or directory: 'test.pyg'
system returned with code 256

! Package minted Error: Missing Pygments output; \inputminted was
probably given a file that does not exist--otherwise, you may need 
the outputdir package option, or may be using an incompatible build tool,
or may be using frozencache with a missing file.

See the minted package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.33 \end{tcblisting}

?

Here is my file list:

 *File List*
 article.cls    2021/10/04 v1.4n Standard LaTeX document class
  size10.clo    2021/10/04 v1.4n Standard LaTeX file (size option)
tcolorbox.sty    2021/06/14 version 4.51 text color boxes
     pgf.sty    2021/05/15 v3.1.9a (3.1.9a)
  pgfrcs.sty    2021/05/15 v3.1.9a (3.1.9a)
pgfrcs.code.tex
 pgfcore.sty    2021/05/15 v3.1.9a (3.1.9a)
graphicx.sty    2021/09/16 v1.2d Enhanced LaTeX Graphics (DPC,SPQR)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
graphics.sty    2021/03/04 v1.4d Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2021/08/11 v1.11 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
  pdftex.def    2020/10/05 v1.2a Graphics/color driver for pdftex
  pgfsys.sty    2021/05/15 v3.1.9a (3.1.9a)
pgfsys.code.tex
pgfsyssoftpath.code.tex    2021/05/15 v3.1.9a (3.1.9a)
pgfsysprotocol.code.tex    2021/05/15 v3.1.9a (3.1.9a)
  xcolor.sty    2021/10/31 v2.13 LaTeX color extensions (UK)
   color.cfg    2016/01/02 v1.6 sample color configuration
pgfcore.code.tex
pgfcomp-version-0-65.sty    2021/05/15 v3.1.9a (3.1.9a)
pgfcomp-version-1-18.sty    2021/05/15 v3.1.9a (3.1.9a)
verbatim.sty    2020-07-07 v1.5u LaTeX2e package for verbatim enhancements
 environ.sty    2014/05/04 v0.3 A new way to define environments
trimspaces.sty    2009/09/17 v1.1 Trim spaces around a token list
etoolbox.sty    2020/10/05 v2.5k e-TeX tools for LaTeX (JAW)
  minted.sty    2017/07/19 v2.5 Yet another Pygments shim for LaTeX
kvoptions.sty    2020-10-07 v3.14 Key value format for package options (HO)
 ltxcmds.sty    2020-05-10 v1.25 LaTeX kernel commands for general use (HO)
kvsetkeys.sty    2019/12/15 v1.18 Key value parser (HO)
 fvextra.sty    2019/02/04 v1.4 fvextra - extensions and patches for fancyvrb
  ifthen.sty    2020/11/24 v1.1c Standard LaTeX ifthen package (DPC)
fancyvrb.sty    2021/11/24\nobreakspace {} 4.1a\nobreakspace {}verbatim text (t
vz,hv)
 upquote.sty    2012/04/19 v1.3 upright-quote and grave-accent glyphs in verbat
im
  lineno.sty    2005/11/02 line numbers on paragraphs v4.41
    calc.sty    2017/05/25 v4.3 Infix arithmetic (KKT,FJ)
shellesc.sty    2019/11/08 v1.0c unified shell escape interface for LaTeX
ifplatform.sty    2017/10/13 v0.4a Testing for the operating system
pdftexcmds.sty    2020-06-27 v0.33 Utility functions of pdfTeX for LuaTeX (HO)
infwarerr.sty    2019/12/03 v1.5 Providing info/warning/error messages (HO)
   iftex.sty    2020/03/06 v1.0d TeX engine tests
catchfile.sty    2019/12/09 v1.8 Catch the contents of a file (HO)
etexcmds.sty    2019/12/15 v1.7 Avoid name clashes with e-TeX commands (HO)
ifluatex.sty    2019/10/25 v1.5 ifluatex legacy package. Use iftex instead.
   "test.w18"
 xstring.sty    2021/07/21 v1.84 String manipulations (CT)
  framed.sty    2011/10/22 v 0.96: framed or shaded text with page breaks
   float.sty    2001/11/08 v1.3d Float enhancements (AL)
l3backend-pdftex.def    2021-10-18 L3 backend support: PDF output (pdfTeX)
supp-pdf.mkii
epstopdf-base.sty    2020-01-24 v2.11 Base part for package epstopdf
epstopdf-sys.cfg    2010/07/13 v1.3 Configuration of (r)epstopdf for TeX Live
_minted-test/default-pyg-prefix.pygstyle
_minted-test/default.pygstyle
 ***********
muzimuzhi commented 2 years ago

Reproducible with minted + autogobble option.

Direct cause is the inline python script called by minted when autogobble is used: check the runsystem(import os; ... fvstopnum = \FancyVerbStopNum ...) in log. (The backslash character \ is responsible for the python error SyntaxError: unexpected character after line continuation character.) Here \FancyVerbStopNum should expand to its value.

Primary cause is, recent change in fancyvrb modifies the initial definition of \FancyVerbStopNum which makes a flaw in minted uncovered. \FancyVerbStopNum is s (count or dimension) register, but minted uses

\edef\minted@autogobblecmd{%
 ...
  \ifx\FancyVerbStopNum\z@ 0\else\FancyVerbStopNum\fi
  ...
}

It should be \else\the\FancyVerbStopNum\fi.

muzimuzhi commented 2 years ago

I've reported this to minted, see gpoore/minted#316, and you can also find a workaround there.

T-F-S commented 2 years ago

I also reported the fancyvrb problem to minted (https://github.com/gpoore/minted/issues/315) and Geoffry Poore already wrote that it is going to be fixed.

Meanwhile, you can use the workaround of @muzimuzhi or, if autogobble is not relevant for you, setting (temporarily)

\tcbsetforeverylayer{minted options={tabsize=2,fontsize=\small,breaklines,autogobble=false}}

removes the default autogobble feature.

muzimuzhi commented 2 years ago

I've reported this to minted, see gpoore/minted#316, and you can also find a workaround there.

I'm not sure but it seems most of the time you only need to patch \minted@autogobble.

Meanwhile, you can use the workaround of @muzimuzhi or, if autogobble is not relevant for you, setting (temporarily)

\tcbsetforeverylayer{minted options={tabsize=2,fontsize=\small,breaklines,autogobble=false}}

removes the default autogobble feature.

With tcb's hooks library, this can be shorten to \tcbsetforeverylayer{minted options app={autogobble=false}}.

T-F-S commented 2 years ago

Since the official release of the adapted minted package may take some time longer, I will add a temporary patch to the minted library which is based on @muzimuzhi proposal.

muzimuzhi commented 2 years ago

Note my original proposal missed \relax to terminate the \numexpr, see discussions under https://github.com/gpoore/minted/commit/4280e7fb841d2d3487c3b25b130104284ae532da and the fix https://github.com/gpoore/minted/commit/c012fb84343f86c9bc80f39b009e115a0a356d62.

muzimuzhi commented 2 years ago

The missing \relaxes are only needed for the patch for fivesum computation (in \minted@pygmentize), which is a python implementation of md5 computation. The python inline script is only used when an old engine (pdftex in texlive 2005 or older, xetex in texlive 2015 or older) that haven't provided related primitive is used.

This explains my previous finding

I've reported this to minted, see gpoore/minted#316, and you can also find a workaround there.

I'm not sure but it seems most of the time you only need to patch \minted@autogobble.

The following git diff results could account for a minimal fix, while it's totally ok to append \relax to both \minted@use@FancyVerbStartNum and \minted@use@FancyVerbStopNum.

diff --git a/tex/latex/tcolorbox/tcbminted.code.tex b/tex/latex/tcolorbox/tcbminted.code.tex
index 6c3a2ee..e92d728 100644
--- a/tex/latex/tcolorbox/tcbminted.code.tex
+++ b/tex/latex/tcolorbox/tcbminted.code.tex
@@ -36,11 +36,11 @@
     % patch for fivesum computation
     \xpatchcmd*\minted@pygmentize%
       {\ifx\FancyVerbStartNum\z@ 0\else\FancyVerbStartNum\fi}%
-      {\minted@use@FancyVerbStartNum}%
+      {\minted@use@FancyVerbStartNum\relax}%
       {}{}%
     \xpatchcmd*\minted@pygmentize%
       {\ifx\FancyVerbStopNum\z@ 0\else\FancyVerbStopNum\fi}%
-      {\minted@use@FancyVerbStopNum}%
+      {\minted@use@FancyVerbStopNum\relax}%
       {}{}%
     % patch for option "autogobble"
     \xpatchcmd\minted@autogobble%
T-F-S commented 2 years ago

Oh, i missed this. It is too late for 5.0.0, but I will correct this for the next version, if a minted update is not released before. Fortunately, this should not affect too many (none?) people.

muzimuzhi commented 2 years ago

Fortunately, this should not affect too many (none?) people.

Yes.

T-F-S commented 2 years ago

Fixed [5.0.1]