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

[Question] finalizecache and cachedir undefined when using minted with tcblisting #208

Closed mirand863 closed 1 year ago

mirand863 commented 1 year ago

I am using minted to display python code and I need to use finalizecache and cachedir to be able to submit my code somewhere else and compile without using --shell-escape, which is disabled for security reasons. However, when I create a newtcblisting and pass these parameters I get a keyval error saying that these parameters are undefined. This is how I define a newtcblisting:

\usepackage[many]{tcolorbox}
\tcbuselibrary{minted,skins}
\usepackage{caption}

\definecolor{Gray}{gray}{0.9}

\newtcblisting{sourcecode}[1][python]{
  colback=Gray,
  listing engine=minted,
  minted language=#1,
%  minted options={finalizecache,cachedir=.}, % Package keyval Error: finalizecache and cachedir are undefined.
  listing only,
  skin=tile,
  width=0.8\textwidth
}

\captionsetup[table]{name=Code}

Then I create my code like this:

\begin{table}[!htb]
    \centering
    \begin{sourcecode}
from hiclass import LocalClassifierPerNode
from hiclass.metrics import f1
from sklearn.ensemble import RandomForestClassifier

# define mock data
X_train = X_test = [[1, 2], [3, 4]]
Y_train = Y_test = [
    ["Animal", "Mammal", "Cat"],
    ["Animal", "Reptile", "Turtle"],
]

# Use random forest classifiers for every node
rf = RandomForestClassifier()
lcpn = LocalClassifierPerNode(local_classifier=rf)
lcpn.fit(X_train, Y_train)  # Train model
predictions = lcpn.predict(X_test)  # Predict test data

# Print hierarchical F-score
print(f"f1: {f1(y_true=Y_test, y_pred=predictions)}")
    \end{sourcecode}
    \caption{Example on how to use \texttt{HiClass} to train and evaluate a hierarchical classifier.}
    \label{code}
\end{table}

Which gives me the output below:

Code generated with minted

Am I doing something wrong when creating this tcblisting? Other parameters such as linenos work fine.

Edit: question also asked on stackexchange

T-F-S commented 1 year ago

Edit: question also asked on stackexchange

Is the question obsolete now, because the stackexchange gives Page not found. This question was voluntarily removed by its author. ?

mirand863 commented 1 year ago

It was not getting views so I deleted. Undeleted now. :)

T-F-S commented 1 year ago

The options finalizecache and cachedir are package options according to the minted documentation, i.e. you have to use these options when including the package.

You could do the following:

\PassOptionsToPackage{finalizecache,cachedir=.}{minted}
\documentclass{article}

\usepackage[many]{tcolorbox}
\tcbuselibrary{minted,skins}
\usepackage{caption}

\definecolor{Gray}{gray}{0.9}

\newtcblisting{sourcecode}[1][python]{
  colback=Gray,
  listing engine=minted,
  minted language=#1,
  %minted options={finalizecache,cachedir=.}, % Package keyval Error: finalizecache and cachedir are undefined.
  listing only,
  skin=tile,
  width=0.8\textwidth
}

\captionsetup[table]{name=Code}

\begin{document}

\begin{table}[!htb]
    \centering
    \begin{sourcecode}
from hiclass import LocalClassifierPerNode
from hiclass.metrics import f1
from sklearn.ensemble import RandomForestClassifier

# define mock data
X_train = X_test = [[1, 2], [3, 4]]
Y_train = Y_test = [
    ["Animal", "Mammal", "Cat"],
    ["Animal", "Reptile", "Turtle"],
]

# Use random forest classifiers for every node
rf = RandomForestClassifier()
lcpn = LocalClassifierPerNode(local_classifier=rf)
lcpn.fit(X_train, Y_train)  # Train model
predictions = lcpn.predict(X_test)  # Predict test data

# Print hierarchical F-score
print(f"f1: {f1(y_true=Y_test, y_pred=predictions)}")
    \end{sourcecode}
    \caption{Example on how to use \texttt{HiClass} to train and evaluate a hierarchical classifier.}
    \label{code}
\end{table}

\end{document}
mirand863 commented 1 year ago

@T-F-S,

That worked perfectly. Thank you very much!