Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tcblisting, hbox and minted #2

Closed
georgejean opened this issue Feb 28, 2015 · 16 comments
Closed

tcblisting, hbox and minted #2

georgejean opened this issue Feb 28, 2015 · 16 comments
Assignees

Comments

@georgejean
Copy link

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
Copy link
Owner

T-F-S commented Mar 12, 2015

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
Copy link
Author

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

@muzimuzhi
Copy link
Contributor

muzimuzhi commented Sep 18, 2020

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.

  • It is python library pygments which minted depends that produces the tex file using Verbatim env.
  • One can change the env name to <name> by using command line option -P envname=<name> of executable pygmentize, see related doc of pygments.
  • Code \minted@def@optcl{envname}{-P envname}{#1} adds a minted option envname which does the job on minted side.
  • Code \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).

@T-F-S
Copy link
Owner

T-F-S commented Sep 22, 2020

@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
Copy link
Owner

T-F-S commented Sep 28, 2020

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

@T-F-S T-F-S closed this as completed Oct 12, 2020
@georgejean
Copy link
Author

georgejean commented Nov 5, 2020

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
Copy link
Owner

T-F-S commented Nov 5, 2020

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
Copy link
Author

Ok, thank you for this explanation and these details.

@muzimuzhi
Copy link
Contributor

An alternative implementation that minted uses for its \mintinline (also see gpoore/minted#281 (comment)):

\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
Copy link
Contributor

muzimuzhi commented Aug 26, 2024

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 gpoore/minted@31f09c8 and gpoore/minted#281 (comment)).

% to be removed, if integrated into the minted package (#281)
\minted@def@optcl{envname}{-P envname}{#1}

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
Copy link

gpoore commented Aug 26, 2024

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
Copy link
Contributor

@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
Copy link
Owner

T-F-S commented Sep 18, 2024

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
Copy link
Contributor

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
Copy link
Owner

T-F-S commented Sep 18, 2024

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
Copy link
Owner

T-F-S commented Sep 27, 2024

% 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants