0

So I've been loosing my mind over typesetting a document with some code examples. After a few days of investigations I was able to narrow down the issue.

This is the preamble (edit: shorter code):

\documentclass[11pt,a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage{tocloft}
\usepackage[most,listings,minted]{tcolorbox}
\usepackage[english]{babel}
\newtcblisting[auto counter,number within=chapter,list inside=loe]{cxmpl}[2]{listing engine=minted,minted language=#1,minted options={breaklines,autogobble,linenos,fontsize=\small,numbersep=3mm},title=\textbf{Code~\thetcbcounter:} #2}
\newtcbtheorem[use counter from=cxmpl,list inside=loe]{xmpl}{Example}{}{xmp}
\begin{document}
    \chapter{One}
        \begin{minted}{csharp}
int main() {printf("hello, world");return 0;}
        \end{minted}
        \begin{cxmpl}{csharp}{A different code}
int main() {printf("hello, northpole");return 0;}
        \end{cxmpl}
        \begin{xmpl}{An example}
            this is just an example
        \end{xmpl}
        \begin{minted}{csharp}
int main() {printf("hello, sun");return 0;}
        \end{minted}
        \begin{cxmpl}{csharp}{Some different code}
int main() {printf("hello, woods");return 0;}
        \end{cxmpl}
\end{document}

produces the following output (without errors):

enter image description here

In 1.2 the "t" is missing and 1.1 and 1.3 shows not the right code.

Typesetting the same code without the minted environment produces this output:

![enter image description here

Typesetting after deleting all the files and folders produces the following errors and output:

enter image description here

enter image description here

The strange thing is, that the console shows this message for every cxmpl-environment. However they don't show up in the log:

Python was not found but can be installed from the Microsoft Store: https://go.microsoft.com/fwlink?linkID=2082640

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

For now I'm quite lost, because I cannot explain to myself where the issue could come from.

6
  • It would be nice if you could remove all the unneccessary options in your environments and post one minimal, self-contained example, from \documentclass to \end{document}. We don't mind the formatting here, we only want to analyze and get rid of the error. It's much easier when it is isolated. Thanks.
    – frougon
    Apr 6, 2020 at 9:41
  • Well, according to the consoe message, first, do you have python and pygments module installed? If so, is python in your PATH? Alternatively, is minted working OK without tcolorbox? Apr 6, 2020 at 10:03
  • May be your problem is related with this one
    – Ignasi
    Apr 6, 2020 at 11:07
  • when using just the minted environment, it works fine. The code changes and the coloring is correct
    – m.dec
    Apr 6, 2020 at 11:07
  • @Ignasi unfortunately it's not related. It's not the first word or command that's missing, like in that Question, but only the first letter. Thank you anyways.
    – m.dec
    Apr 6, 2020 at 11:12

2 Answers 2

2

Welcome to TeX.SE, and thanks for making your example both shorter and self-contained (you could still remove a few packages for the sake of the question here, but that is a good start). There are two unrelated problems in your code.

The first problem has nothing to do with listings; you are simply misusing the xmp environment defined with \newtcbtheorem. Such environments take an additional argument that is used to define a label for the box produced by tcolorbox (together with /tcb/label separator and the fourth mandatory argument passed to \newtcbtheorem). See Library theorems in the tcolorbox manual (page 350 in version 4.22). In short, you have to call the xmp environment like this:

\begin{xmpl}{An example}{some-possibly-empty-label}
    this is just an example
\end{xmpl}

In your case, you were not passing the some-possibly-empty-label argument, therefore the xmpl environment grabbed the next token as the argument, which was the letter t in your case.

The second problem is that you didn't use listing only in the \newtcblisting options when defining your cxmpl environment. As a result, your code was first typeset using minted (verbatim and colored), then also interpreted as LaTeX code for the lower part of the tcolorbox (you noticed the horizontal dashed line, didn't you? It separates the upper part from the lower part). That is why the braces had disappeared in the lower part: for TeX under standard category code régime, they only denote beginning of group and end of group; this does not result in typesetting of braces. The default setting is well suited for samples of LaTeX code where it is nice to see the formatted output next to the code. For other languages, listing only is more appropriate in general.

Here is the code with the two issues fixed:

\documentclass{book}
\usepackage{geometry}
\usepackage[listings, minted, theorems]{tcolorbox}

\newtcblisting[auto counter, number within=chapter, list inside=loe]{cxmpl}[2]{
  listing engine=minted, minted language={#1}, listing only,
  minted options={breaklines, autogobble, linenos, fontsize=\small,
                  numbersep=3mm},
  title={\textbf{Code~\thetcbcounter:} #2},
}

\newtcbtheorem[use counter from=cxmpl, list inside=loe]{xmpl}{Example}{}{xmp}

\begin{document}

\chapter{One}

  \begin{minted}{csharp}
int main() {printf("hello, world"); return 0;}
  \end{minted}

  \begin{cxmpl}{csharp}{A different code}
int main() {printf("hello, northpole"); return 0;}
  \end{cxmpl}

  \begin{xmpl}{An example}{some-possibly-empty-label}
      This is just an example.
  \end{xmpl}

  \begin{minted}{csharp}
int main() {printf("hello, sun"); return 0;}
  \end{minted}

  \begin{cxmpl}{csharp}{Some different code}
int main() {printf("hello, woods"); return 0;}
  \end{cxmpl}

\end{document}

enter image description here

2
  • Thank you very much for your answer. I could learn a few things from that. Unfortunately typesetting the exact same code as you suggested, still produced the same issue with listings. However this means the problem lies elsewhere. I guess either something with tcolorbox passing the code to python and python not having enough privileges, maybe something with some temporary files or something wit the miktex/Texmaker installation or the packages. However I updated everything on Friday, so I should be able to exclude the last option
    – m.dec
    Apr 6, 2020 at 14:07
  • The code I posted does generate the output from my answer. Your two errors Python was not found but can be installed from the Microsoft Store and Error: cannot read infile: [Errno 2] No such file or directory: 'test_pygments.pyg' are Python and pygments-related, presumably coming from an installation problem. They have nothing to do with LaTeX per se, and I'm afraid there is not much I can do myself about them.
    – frougon
    Apr 6, 2020 at 14:23
1

So after doing some research and some error and trial I found the following solution:

Somehow, although I added the python-directory manually to the path variable and also that of the scripts, the command prompt did not recognize python (it didn't show anything after pressing enter and opened the windows store, which could explain the error Python was not found but can be installed from the Microsoft Store: https://go.microsoft.com/fwlink?linkID=2082640). Strangely enough I was able to install and run pip and pygments (which maybe explains why the minted package was working fine)

To solve the problem I completely reinstalled python with the following option:

enter image description here

Then entering python results in

enter image description here

Finally, I installed pygments and reran LaTeX, which still produced the wrong output.

The last issue was just because there where still all the old files. Deleting them and rerunning LaTeX solved the issue for me.

enter image description here

Hope this turns out useful also for someone else in future.

1
  • Glad to see you managed to get rid of the remaining issues, and nicely explained how you could solve the non-LaTeX problems (or “incorrect output due to intermediate files from a previous run” problems). :-)
    – frougon
    Apr 7, 2020 at 10:21

You must log in to answer this question.

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