Closed georgejean closed 4 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...
Thanks for your answer. I will then use listings
instead if I need it. Thanks for this package.
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}
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
.
pygments
which minted
depends that produces the tex file using Verbatim
env.<name>
by using command line option -P envname=<name>
of executable pygmentize
, see related doc of pygments
. \minted@def@optcl{envname}{-P envname}{#1}
adds a minted
option envname
which does the job on minted
side.\tcbset{minted options app={envname=BVerbatim}}
configures tcblisting
env to make use of that new option.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)
.
@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.
Implemented in version 4.40 (2020/09/25).
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) Is this just a limitation of the hbox capture mode?
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.
Ok, thank you for this explanation and these details.
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
.
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).
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}}{}
}
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
.
@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.
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}
}
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.
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.
% 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
My OS is Windows 8.1, 64 bits and I use MikTeX 2.9. I want to use the capture mode
hbox
in atcblisting
environment withminted
but it seems it doesn't work (but it does well withlistings
). Here is my code (with and withouthbox
), the result, and the error log. Thanks for any suggestions.