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

tcblisting, hbox and minted #2

Closed georgejean closed 4 years ago

georgejean commented 9 years ago

My OS is Windows 8.1, 64 bits and I use MikTeX 2.9. I want to use the capture mode hbox in a tcblisting environment with minted but it seems it doesn't work (but it does well with listings). Here is my code (with and without hbox), the result, and the error log. Thanks for any suggestions.

\documentclass[a4paper,oneside,12pt]{article}
\usepackage{tcolorbox}
\tcbuselibrary{minted,skins}

\begin{document}

% standard colorbox with code (without hbox)
\begin{tcblisting}{listing only, minted style = emacs, minted language = python}
from math import sin, exp

def Suite(n):
    U = 3
    for k in range(n):
        U = sin(U)+1/(exp(U)+1)
    return U

# Example
Suite(13)
\end{tcblisting}

% standard colorbox with code (with hbox. It doesn't work)
\begin{tcblisting}{listing only, minted style = emacs, minted language = python, hbox}
from math import sin, exp

def Suite(n):
    U = 3
    for k in range(n):
        U = sin(U)+1/(exp(U)+1)
    return U

# Example
Suite(13)
\end{tcblisting}

\end{document}

issue

! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                                                                         
l.1 ...dchars=\\\{\}, ,tabsize=2,fontsize=\small ]                                            
Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.
! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                                                                        
l.1 ...dchars=\\\{\}, ,tabsize=2,fontsize=\small ]                                            
Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.
! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                                                                        
l.12 \end{Verbatim}            
Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.
! LaTeX Error: Something's wrong--perhaps a missing \item.
See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                                                                           
l.12 \end{Verbatim}               
Try typing  <return>  to proceed.
If that doesn't work, type  X <return>  to quit.
)
runsystem(del essaiMintedTcolor.out.pyg)...executed.
Overfull \hbox (3913.37741pt too wide) in paragraph at lines 33--33
T-F-S commented 9 years ago

Sorry for the delay. Unfortunately, I have no good answer to this. While listings can be put into a \hbox (which is used internally in your code), minted cannot. I don't know how to change that...

georgejean commented 9 years ago

Thanks for your answer. I will then use listings instead if I need it. Thanks for this package.

muzimuzhi commented 4 years ago

This is feasible by extending minted a little bit.

Full working example:

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{hooks, minted}

\makeatletter
% "envname=<key>" will add "-P envname=<key>" as CLI option of "pygmentize"
\minted@def@optcl{envname}{-P envname}{#1}
\makeatother

\tcbset{minted options app={envname=BVerbatim}}

\begin{document}
\begin{tcblisting}{listing only, hbox}
  $a + b = c$
\end{tcblisting}
\end{document}

image

Explanations

The restriction here is that, by default minted puts highlighted codes in Verbatim env (provided by fancyvrb and refined by fvextra package), and Verbatim env cannot be put inside an hbox. fancyvrb has a BVerbatim env which suits the requirement of tcb option capture=hbox. So the aim is to change env name from Verbatim to BVerbatim.

For better user experience, some logic might be added to the internals of tcblisting env, to automatically apply envname=BVerbatim when (listing engine == minted) and (capture mode == hbox).

T-F-S commented 4 years ago

@muzimuzhi Very nice solution. I reopened this issue since an answer is now given.

I tested your code and everything went smooth. Automated option setting is also possible and no further user interaction is needed, if hbox is selected.

T-F-S commented 4 years ago

Implemented in version 4.40 (2020/09/25).

georgejean commented 4 years ago

When I use this code


\documentclass[dvipsnames,a4paper,oneside,12pt]{article}
\usepackage{amsfonts}
\usepackage[a4paper]{geometry}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{tcolorbox}
\usepackage[french]{babel}
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}

\tcbuselibrary{minted,breakable,skins}

\definecolor{matrix}{RGB}{85, 255, 85}
\newtcblisting{myScilab}[1]{listing engine=minted, minted language=scilab,
minted options={fontsize=\small,linenos,numbersep=3mm},
minted style = rainbow_dash,colback=matrix!3!white,colframe=gray!75!black,
listing only,
sharp corners,
left=5mm,enhanced,
drop fuzzy shadow = gray,
overlay={\begin{tcbclipinterior}\fill[gray!20!white] (frame.south west)
rectangle ([xshift=5mm]frame.north west);\end{tcbclipinterior}},
#1}

\begin{document}

\begin{myScilab}{}
// say hello
disp("hello")
\end{myScilab}

\begin{myScilab}{hbox}
// say hello
disp("hello")
\end{myScilab}

\end{document}

I get this output (no line numbers with hbox) Capture2 Is this just a limitation of the hbox capture mode?

T-F-S commented 4 years ago

With hbox, the Verbatim environment from fancyvrb is replaced by BVerbatim. http://mirrors.ctan.org/macros/latex/contrib/fancyvrb/doc/fancyvrb-doc.pdf states on page 17:

4.2.2 BVerbatim environment This environment puts the verbatim material in a TEX box. Some parameters do not work inside this environment (notably the framing ones), but two new ones are available

Line numbering is one of the things which are not supported by BVerbatim (by testing).

Maybe, the package author of fancyvrb can add line numbering to BVerbatim (?). I do not know, if this is an easy or impossible task.

georgejean commented 4 years ago

Ok, thank you for this explanation and these details.

muzimuzhi commented 3 years ago

An alternative implementation that minted uses for its \mintinline (also see https://github.com/gpoore/minted/issues/281#issuecomment-973582998):

\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{hooks, minted}

\begin{document}
\begin{tcblisting}{listing only, hbox, 
  before app={\RecustomVerbatimEnvironment{Verbatim}{BVerbatim}{}}}
  $a + b = c$
\end{tcblisting}
\end{document}

This avoids adding options to minted, and hence is as robust (or fragile?) as \mintinline.

muzimuzhi commented 2 months ago

The under-developing minted v3 has added the envname option. Due to the extensive refactoring v3 has took, including switching to pgfkeys package for key-value mechanism, \minted@def@optcl is redefined as a no-op (see https://github.com/gpoore/minted/commit/31f09c8a2a4defdb432a30954ecf0c25444c3d67 and https://github.com/gpoore/minted/issues/281#issuecomment-2308940274).

https://github.com/T-F-S/tcolorbox/blob/f7f39a4af09b6cce028c0e8e83d6c40f56d899d1/tex/latex/tcolorbox/tcbminted.code.tex#L25-L26

Although minted v3 is not released yet, something like

-\minted@def@optcl{envname}{-P envname}{#1}
+% 2024/08/25 is the date on which minted supported "envname" option, not the v3 released date
+\IfPackageAtLeastTF{minted}{2024/08/25}{}{
+  \minted@def@optcl{envname}{-P envname}{#1}
+}

would make future releases of tcolorbox compatible with both minted v2.x and v3+.

Update: Hmm the historic minted v2.x may also be loaded by \usepackage{minted2}, using a new minted2.sty package which will ship with release v3.

Se the compatibility code has to be a little more complex:


\IfPackageLoadedTF{minted2}{
  \minted@def@optcl{envname}{-P envname}{#1}
}{
  \RequirePackage{minted}
  \IfPackageAtLeastTF{minted}{2024-08-25}
     {\minted@def@optcl{envname}{-P envname}{#1}}{}
}
gpoore commented 2 months ago

No immediate changes to tcolorbox should be needed for minted v3+ compatibility, since the new version of minted creates \minted@def@optcl to avoid macro-not-defined errors, and this will be present for at least a year or two. A separate test for the new minted2 package probably isn't necessary, since minted2 attempts to completely impersonate minted v2.9 so it should pass the \IfPackage... tests for minted.

muzimuzhi commented 2 months ago

@gpoore You're totally right. I missed the emulations in minted2.sty

\@namedef{ver@minted.sty}{2023/12/18 v2.9 Yet another Pygments shim for LaTeX}
\expandafter\let\expandafter\minted@tmp\csname opt@minted2.sty\endcsname
\expandafter\let\csname opt@minted.sty\endcsname\minted@tmp

BTW LaTeX provides a (light-weight) package rollback mechanism since LaTeX2e 2018-04-01. It enables using \usepackage{mypkg}[=v2] to load a historic version of mypkg.sty stored in another file, see a complete example in this gist.

T-F-S commented 2 months ago

Thank you for reporting this future change of minted.

If I get the discussion right, the first proposal of @muzimuzhi should be added to avoid problems in one or two years when minted drops \minted@def@optcl , i.e.

% 2024/08/25 is the date on which minted supported "envname" option, not the v3 released date
\IfPackageAtLeastTF{minted}{2024/08/25}{}{
  \minted@def@optcl{envname}{-P envname}{#1}
}
muzimuzhi commented 2 months ago

The incoming minted v3 won't break tcolorbox's use of envname minted option. It just makes the \minted@def@optcl... line in tcolorbox do nothing and raise a warning "Macro \minted@def@optcl is deprecated with minted v3 and no longer has any effect".

The effect of my first proposal is to silence that warning.

T-F-S commented 2 months ago

The effect of my first proposal is to silence that warning.

And to avoid errors when Geoffrey Poore removes the deprecated code in the near or far future 👍

I will add this to the next version.

T-F-S commented 1 month ago
% 2024/08/25 is the date on which minted supported "envname" option, not the v3 released date
\IfPackageAtLeastTF{minted}{2024/08/25}{}{
  \minted@def@optcl{envname}{-P envname}{#1}
}

Added to https://github.com/T-F-S/tcolorbox/releases/tag/v6.4.0