[Tex/LaTex] Underscore in filenames in \textit{} imported from a python script


I'm trying to import the name of a result file from a python script and displaying it in the report. In the LaTeX template I have a bunch of variables defined as ${variablename}. The python script changes this into the actual variable values. I only have problem with one variable, the filename.

The code works as long as the filename doesn't contain any underscores.
I've tried loosing the \texit{} command, using \StrSubstitute (with \\_, \bs and \textunderscore) and math mode. The only thing that kinda worked was math mode, but I want it displayes as text. With math mode the file name was so long that it continued far off the page. With \textit{} and no underscores it splits the filename, just like I want it.

I'm at loss.


\usepackage{booktabs} % professional tables
\usepackage{gensymb} % \degree and \celsius, try '$ texdoc gensymb'
\usepackage{amsmath, amsfonts, amssymb} % mathematical symbols
\usepackage{float, longtable, graphicx}

% end of preamble, start of document


 -Code irrelevant to problem-

The test results are taken from the file named \textit{${RESFILEname}}

-More irrelevant code-


Best Answer

Perhaps the most robust solution for filenames is to add


Then use \path{my_file_name} The url package commands take care of special symbols, and allow the line to break. You can customise the font used, see the comments in the file.

Related Question