# 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. \documentclass[a4paper,11pt,titlepage]{article} \usepackage[utf8]{inputenc} \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

\begin{document}

-Code irrelevant to problem-

\section{Results}\label{sec:results}
The test results are taken from the file named \textit{${RESFILEname}} -More irrelevant code- \end{document}  - Welcome to TeX.SX! You can have a look on our starter guide to familiarize yourself further with our format. – karlkoeller Jun 27 '13 at 9:07 ## 2 Answers Perhaps the most robust solution for filenames is to add \usepackage{url}  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. - i find it easier to read the comments in the file, but i'm "unusual". url.sty is one of the packages i've generated tex->pdf docs for. $MLC/url/url.pdf on ctan, texdoc url on my tl machine –  wasteofspace Jun 27 '13 at 9:33
David: Thanks. That worked! The actual reference doesn't work, but that's not too big of a problem. I also added urlcolor=black so it wouldn't stand out so much –  huyx Jun 27 '13 at 10:07

You can use the package url as stated by David Carlisle, but if you want your filenames to be printed in italic, I suggest to define a custom command with a custom font.

\usepackage{url}
\def\UrlFont{\it}                         % italic shape for the used font
\DeclareUrlCommand\itfile{\urlstyle{it}}  % declare new command \itfile


and then replace \textit{${RESFILEname}} with \itfile{${RESFILEname}}, so to have

\documentclass[a4paper,11pt,titlepage]{article}
\usepackage[utf8]{inputenc}

\usepackage{booktabs} % professional tables
\usepackage{gensymb} % \degree and \celsius, try '$texdoc gensymb' \usepackage{amsmath, amsfonts, amssymb} % mathematical symbols \usepackage{float, longtable, graphicx} \usepackage{url} \def\UrlFont{\it} % italic shape for the used font \DeclareUrlCommand\itfile{\urlstyle{it}} % declare new command \itfile % end of preamble, start of document \begin{document} -Code irrelevant to problem- \section{Results}\label{sec:results} The test results are taken from the file named \itfile{${RESFILEname}}

-More irrelevant code-

\end{document}

-