I'm not a Latex expert, so I reached the limit of my knowledge in the tracking down of this problem. If someone can shed some light into this problem I think it would be very useful.

The Problem Description (applicable on Windows, see environment bellow: When using \newtcbinputlisting from \tcolorbox with minted library, IF the file name has Spaces and/or underscores, the end result and/or compilation may fail or have unexpected behavior. The problem has variants if single or multiple snippets of SAME file are used. I will present some of tested conditions... but others may (probably) exist.

The Environment: I'm running Windows 10 Education, 64-bit, build 17134; MiKTeX: 2.9.6930; minted 2.5; tcolorbox 4.15.

Setup of MWE: To have a MWE use the code bellow saved as minimal.tex. Then use the following batch-file (see bellow) to create all required files (It's just multiple copies of minimal.tex)

How to run and Identify the problem:

  1. (OPTIONAL) Uncomment section with \inputminted command; COMMENT section with \setCode command. 1.a - RESULT: You should see eight listings with NO problem. (I think that this ensures that the problem Is Not from minted package. Do you agree?)
  2. IMPORTANT: Delete directory "_minted-minimal" (the minted cache) if you run the Step 1.
  3. IMPORTANT: Comment ALL \inputminted if you run Step 1.
  4. Comment \setCode for Code 1a to Code 4a (the last four). 4.a - RESULT: You should see FOUR listings, with NO Problems. 4.b - VARIANT WITH PROBLEMS: As you can see in title, the use OR not of double quotes is important... and is dependent of the existence or not os spaces/underscores in the file name. Adding/removing the double quotes changes the result. As a "general" rule, filenames WITH spaces MUST have quotes, while WITHOUT space MUST NOT have quotes. (Should check if filename has space... and add double quotes?)
  5. IMPORTANT: Delete directory "_minted-minimal" (the minted cache). This ensures a "Fresh" start for next Scenario: More than one snippet per-source file.
  6. Uncomment \setCode for Code 1a to Code 4a (the last four). 4.a - RESULT: The document is produced, but: A Python error (see bellow) is issued. In Code 6 the missing double quotes creates an "artifact" in the listing (the remainder of the file name after the space).

Things to be Aware: To fully understand the problem several test must be made by adding/removing double quotes for each situation. Since the "delicateness" of results vs input/start conditions, some key aspect MUST be checked:

  • (MOST IMPORTANT) Having or Not the minted cache (_minted-minimal directory) at compilation time INFLUENCES the result, so BE SURE to remove it to test Insertion of FIRST Snippet.
  • Note that a Space-after-Underscore produce a DIFFERENT result that Underscore-after-Space.
  • Note that Same-FILE snippets MAY produce Python error (depending on double quotes use!!)
  • As an additional complication... notice that underscore Is Never escaped... BUT... if \underscore package is used... only the THREE LAST Code snippets are produced on First compilation... AND FOUR afterwards...

Related/Duplicate I have found some potential duplicate/related (in the sense that the root cause may be the same!?) and I even submitted a bug on tcolorbox (now closed... because in fact double quote sometimes overcome the problem). enter link description here Listing of a file with underscores in its name/path Problems setting up minted and tcolorbox to include source code and set title Have just the file name show up in a listing environment

Batch file for setup

mkdir lst
copy minimal.tex "./lst/mintsn_iptest.tex.01"
copy minimal.tex "./lst/mints niptest.tex.01"
copy minimal.tex "./lst/mints n_iptest.tex.01"
copy minimal.tex "./lst/mints_n iptest.tex.01"

copy minimal.tex "./lst/sn_iptest.tex.01"
copy minimal.tex "./lst/s niptest.tex.01"
copy minimal.tex "./lst/s n_iptest.tex.01"
copy minimal.tex "./lst/s_n iptest.tex.01"

The Python error

Process started: pdflatex.exe -synctex=1 -interaction=nonstopmode -shell-escape "minimal".tex

File "<string>", line 1 import sys; import os; import textwrap; from io import open; fname = os.path.expanduser(os.path.expandvars(""./lst/sn_iptest.tex.01"")); f = open(fname, "r", encoding="UTF8") if os.path.isfile(fname) else None; t = f.readlines() if f is not None else None; t_opt = t if t is not None else []; f.close() if f is not None else None; tmpfname = os.path.expanduser(os.path.expandvars("minimal.pyg")); f = open(tmpfname, "w", encoding="UTF8") if t is not None else None; fvstartnum = 10; fvstopnum = 15; s = fvstartnum-1 if fvstartnum != 0 else 0; e = fvstopnum if fvstopnum != 0 else len(t_opt); [f.write(textwrap.dedent("".join(x))) for x in (t_opt[0:s], t_opt[s:e], t_opt[e:]) if x and t is not None]; f.close() if t is not None else os.remove(tmpfname); ^ SyntaxError: invalid syntax

The MWE (Also used as external listing file... renamed)


% General settings
\usepackage[utf8]{inputenc}     % Input encoding
\usepackage[T1]{fontenc}        % Output encoding

% Source Code Style using mint package and tcolorbox package
%   Preamble Setting for Code Listings using {tcolorbox} and {minted} packages
\usepackage[minted,skins,breakable]{tcolorbox}  % tcolorbox with extra packages

\newtcbinputlisting[auto counter, number within=chapter, list inside=srccode,
number freestyle={\noexpand\textbf{Code \noexpand\thechapter.\noexpand\arabic{\tcbcounter}}:}]
{\setCode}[4][]{% Options for the listings (Seven parameters- NO defaults)
    minted options={fontsize=\footnotesize, autogobble, %outputdir=./aux-files/, 
        breaklines, breakanywhere, style=emacs, linenos, #1},
    minted language = tex,
    title ={\textbf{\thetcbcounter} #3},
    listing file = {#2},
    label = {#4},
    listing only,



    Start document here with something simple:
    % No spaces

    First... a control sequence (Optional).

    Use minted directly. NOTE the (Required) use of - "".

%   \separator  
%   \inputminted[firstline=5, lastline=10]{tex}{"./lst/mintsn_iptest.tex.01"}
%   \separator
%   \inputminted[firstline=5, lastline=10]{tex}{"./lst/mints niptest.tex.01"}
%   \separator
%   \inputminted[firstline=5, lastline=10]{tex}{"./lst/mints n_iptest.tex.01"}
%   \separator
%   \inputminted[firstline=5, lastline=10]{tex}{"./lst/mints_n iptest.tex.01"}
%   \separator  \par
%   Now a Second Snippet of same files. \par
%   \separator
%   \inputminted[firstline=10, lastline=15]{tex}{"./lst/mintsn_iptest.tex.01"}
%   \separator
%   \inputminted[firstline=10, lastline=15]{tex}{"./lst/mints niptest.tex.01"}
%   \separator
%   \inputminted[firstline=10, lastline=15]{tex}{"./lst/mints n_iptest.tex.01"}
%   \separator
%   \inputminted[firstline=10, lastline=15]{tex}{"./lst/mints_n iptest.tex.01"}
%   \separator

    The above code DOES NOT work WITHOUT the "". --- > No problems Here.

    Now with TColorbox.

    \textbf{ENSURE minted cache is CLEARED...}. Delete the directory \_minted-project.

    Add/Remove double quotes on file names to see different behaviors.

    \setCode[firstline=5, lastline=10]{./lst/sn_iptest.tex.01}{UND. Only: CANNOT Have quotes}{Code:1}

    \setCode[firstline=5, lastline=10]{"./lst/s niptest.tex.01"}{SPACES: WITH Quotes-OK; WITHOUT-'tail' after first space printed in listing}{Code:2}

    \setCode[firstline=5, lastline=10]{"./lst/s n_iptest.tex.01"}{Space-Underscore: MUST HAVE Quotes}{Code:3}

    \setCode[firstline=5, lastline=10]{"./lst/s_n iptest.tex.01"}{Underscore-Space:  WITH Quotes-OK; WITHOUT-'tail' after first space printed in listing}{Code:4}

    Now a second snippet of same files.

    \setCode[firstline=10, lastline=15]{"./lst/sn_iptest.tex.01"}{UND. Only: MAY have Quotes but Python Error on FIRST compile}{Code:1a}

    \setCode[firstline=10, lastline=15]{./lst/s niptest.tex.01}{SPACES: WITH Quotes-Python Error on FIRST compile (but completes ok); WITHOUT-'tail' after first space printed in listing}{Code:2a}

    \setCode[firstline=10, lastline=15]{"./lst/s n_iptest.tex.01"}{Space-Underscore: MUST HAVE Quotes, BUT Python Error on FIRST compile (but completes ok)}{Code:3a}

    \setCode[firstline=10, lastline=15]{"./lst/s_n iptest.tex.01"}{Underscore-Space:  WITH Quotes-Python Error on FIRST compile (but completes ok); WITHOUT-'tail' after first space printed in listing}{Code:4a}

    End document Here.

  • 1
    Well I would avoid spaces at all costs. Underscores should normally work, if not then make a shorter example that demonstrates the problem. – Ulrike Fischer Feb 11 at 14:43
  • Spaces in file names are ugly. – Christian Hupfer Feb 11 at 16:38
  • The goal of this question is to try to identify a problem (that I think is) on tcolorbox package... which is a quite important one. Regarding spaces (and underscores)... unfortunately filenames and paths have it... I can (and... to overcome the problem do it) rename the files, but that is manageable only for a handful of files and quite prone to errors. Finally... it is always better to solve a problem that find a path to go around. – acombo Feb 12 at 8:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.