2

I would like to know how to go about adding a title bar to a minted block of python code? This is an example of a title bar I would like to use:

enter image description here

I tried using the code from this solution but I could not get the code posted in that solution to compile using WinEdt with the pdftex -shell-escape. When I run the code from the posted solution, I get this error:

Error: cannot read infile: [Errno 2] No such file or directory: 'codeboxes_1.pyg'

! Package minted Error: Missing Pygments output; \inputminted was probably given a file that does not exist--otherwise, you may need the outputdir package option, or may be using an incompatible build tool, or may be using frozencache with a missing file.

Here is my code thus far:

CODE

\documentclass[letter, 12pt]{report}   

%###########################         FOR PYTHON         ####################################
% https://www.overleaf.com/learn/latex/Code_Highlighting_with_minted
\usepackage{minted}
\usepackage{xcolor} % to access the named colour LightGray
\definecolor{LightGray}{gray}{0.9}

\begin{document}

\begin{minted}
[
frame=lines,
framesep=2mm,
baselinestretch=1.2,
bgcolor=LightGray,
fontsize=\footnotesize,
firstnumber = 41,
linenos
]
{python}
fig = plt.subplots(figsize=(12,2))
ax = plt.subplot(1,1,1)
ax.plot(times, x)
ax.grid(True)
plt.ylabel('amplitude [in A.U.]', fontsize=14)
plt.xlabel('time [in sec]', fontsize=14)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()
\end{minted}

\end{document}

1 Answer 1

3

It's hard to see how the code you posted could generate the error message you posted — or did you get that when trying out the code in the linked solution? It looks like you were trying to input code from a file and didn't have that file present.

The actual code you posted compiles for me, but does not have the title bar; for that you need the tcolorbox package.

Below is an attempt to modify the code in the solution you linked to to use for your python code. I didn't bother with the other environments defined in that solution: just the environment that generates code blocks that looks like your desired output.

I added a fourth argument for the starting line number to the code given there.

However, it's still fairly different from the style of the code you posted: are there tweaks you want to make?

\documentclass[letter, 12pt]{report}

\newcounter{commentCount}
\newcounter{filePrg}
\newcounter{inputPrg}

\usepackage[dvipsnames]{xcolor}
\usepackage{minted}

\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}

\usepackage{ifthen}
\usepackage{fontawesome}

\usepackage{tabularx}
\newcolumntype{\CeX}{>{\centering\let\newline\\\arraybackslash}X}%
\newcommand{\TwoSymbolsAndText}[3]{%
  \begin{tabularx}{\textwidth}{c\CeX c}%
    #1 & #2 & #3
  \end{tabularx}%
}

\newtcblisting[use counter=inputPrg, number format=\arabic]{codeInput}[4]{
  listing engine=minted,
  minted language=#1,
  minted options={autogobble,linenos,breaklines,  firstnumber={#4}},
  listing only,
  size=title,
  arc=1.5mm,
  breakable,
  enhanced jigsaw,
  colframe=brown,
  coltitle=White,
  boxrule=0.5mm,
  colback=white,
  coltext=Black,
  title=\TwoSymbolsAndText{\faCode}{%
    \textbf{Input program \thetcbcounter}\ifthenelse{\equal{#2}{}}{}{\textbf{:} \textit{#2}}%
  }{\faCode},
  label=inputPrg:#3
}

\begin{document}

\begin{codeInput}{python}{A sample program.}{code01}{41}
fig = plt.subplots(figsize=(12,2))
ax = plt.subplot(1,1,1)
ax.plot(times, x)
ax.grid(True)
plt.ylabel('amplitude [in A.U.]', fontsize=14)
plt.xlabel('time [in sec]', fontsize=14)
plt.xticks(fontsize=13)
plt.yticks(fontsize=13)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
plt.show()
\end{codeInput}

\end{document}

python with head bar

7
  • Thanks for your solution. I mis-wrote those words...my apologies. My code did not generate the error, it was the code from the link I posted in my question that I generated the error. I will reformat that sentence properly so that it will reflect what code I meant generated the error. Thanks again!
    – Joe
    Oct 10, 2022 at 21:20
  • I am getting the same error when I try your code. Is there a specific library that I am missing that is causing this error? I'm running Windows 10. Thanks!
    – Joe
    Oct 10, 2022 at 21:27
  • 1
    Hard to say. Can you post your complete log file? Is your TeX distribution up to date? Does pygmentize work from the command line?
    – frabjous
    Oct 10, 2022 at 21:35
  • Hello, I got it to work partially by using the command \RequirePackage[cache=false]{minted}, however, for the output generated, I just the boxed header with no python code in there. Let me try if pygmentize works from the command line.\
    – Joe
    Oct 10, 2022 at 21:41
  • 1
    What is the exact command you're running from the command line? That error looks like you tried to use it on a file that doesn't exist, which wouldn't tell us much about how it's working (or not) with minted.
    – frabjous
    Oct 10, 2022 at 22:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .