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

`\tcbusetemplisting` may change global minted style #299

Open muzimuzhi opened 6 days ago

muzimuzhi commented 6 days ago

When listing engine=minted, \tcbusetemplisting may call \usemintedstyle (see code line 36 below) in the current group which then changes "global" minted style.

https://github.com/T-F-S/tcolorbox/blob/b59111a7429eccf196e716edec170f52fe560232/tex/latex/tcolorbox/tcbminted.code.tex#L34-L56

Instead of \usemintedstyle{<style>}, the minted style can also be passed on to \inputminted using style=<style> option. This ensures the setting of minted style is local.

Example

% python snippet is copied from https://pygments.org/styles/
\begin{filecontents}[force, noheader]{example.py}
from typing import Iterator
\end{filecontents}

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

\tcbset{minted style=xcode, minted language=python}

\begin{document}
% style=default
\inputminted{python}{example.py}

% style=xcode
\tcbinputlisting{listing only, listing file=example.py}

% style=xcode
\tcbset{tempfile=example.py}
\tcbusetemplisting

% ERROR style=xcode, should be default
\inputminted{python}{example.py}
\end{document}

image

Proposal

diff --git a/tex/latex/tcolorbox/tcbminted.code.tex b/tex/latex/tcolorbox/tcbminted.code.tex
index 60de31f..e51e6e2 100644
--- a/tex/latex/tcolorbox/tcbminted.code.tex
+++ b/tex/latex/tcolorbox/tcbminted.code.tex
@@ -33,9 +33,10 @@

 \cs_new_nopar:Npn \__tcobox_minted_input_listing:nnnn #1#2#3#4
   {
+    \tl_set:Nn \l__tcobox_tmpa_tl { \inputminted[#1 }
     \tl_if_empty:nF { #4 }
       {
-        \usemintedstyle { #4 }
+        \tl_put_right:Nn \l__tcobox_tmpa_tl { ,style=#4 }
       }
     \tl_set:Nn \l__tcobox_tmpa_tl { \inputminted[#1 }
     \cs_if_exist:NT \tcb@listing@capture
muzimuzhi commented 6 days ago

Note that changing /tcb/minted style=<style> to be an abbreviation for minted options={style=<style>} would make minted options and minted style cancel each other's effect, thus introduce a breaking change.

Although that would be more consistent with the behaviors of listings options and listings style. https://github.com/T-F-S/tcolorbox/blob/b59111a7429eccf196e716edec170f52fe560232/tex/latex/tcolorbox/tcblistings.code.tex#L66

muzimuzhi commented 6 days ago

Ah, the current doc of /tcb/minted style added between v2.50 (2013/07/29) and v3.05 (2014/05/28) does say

Note that styles are always applied globally; all following examples will be set in the given \meta{style} until a new style is set.

But after minted v2.0alpha (2013/07/30) introduced style option, this restriction can be avoided.

Also, the current doc is vague about when minted style is actually applied.

T-F-S commented 6 days ago

Note that changing /tcb/minted style=<style> to be an abbreviation for minted options={style=<style>} would make minted options and minted style cancel each other's effect, thus introduce a breaking change.

Although that would be more consistent with the behaviors of listings options and listings style.

https://github.com/T-F-S/tcolorbox/blob/b59111a7429eccf196e716edec170f52fe560232/tex/latex/tcolorbox/tcblistings.code.tex#L66

The difference is that a listings style can be used as a shortcut for all kinds of listings options while minted style only sets the Pygments stylesheet and not other minted options. Therefore, minted style and minted options are complementary (not really, since we have style as an minted option).

So, I would not like to change /tcb/minted style=<style> to be an abbreviation for minted options={style=<style>}. By the way, the minted documentation describes tcblisting with these options (currently, on page 19) and I would not want to break this.

T-F-S commented 6 days ago

Your proposal from the first post sounds reasonable to me. The global effect is documented and changing to your proposal would mean a possibly breaking change. But, honestly, I do not think that anybody has code which relies on the effect (?)...

Currently, I have no time to test the code, but I will do in the next days. If nothing special comes up, I will do these changes.

Thank you for your ideas!

muzimuzhi commented 6 days ago

The difference is that a listings style can be used as a shortcut for all kinds of listings options while minted style only sets the Pygments stylesheet and not other minted options.

Good point! I am too unfamiliar with listings package.

So, I would not like to change /tcb/minted style=<style> to be an abbreviation for minted options={style=<style>}.

Changing /tcb/minted style=<style> to be an abbreviation is also not the way I would suggest. https://github.com/T-F-S/tcolorbox/issues/299#issuecomment-2458643527 was merely about what I just read from the source code. Sorry I didn't express myself clearly enough.