[Tex/LaTex] Hyphenation when using \verb command

hyphenationline-breakingmacrostypewriterverbatim

I'm having a problem using \verb command.

Usually when I want some verbatim font in the current line (not in a new paragraph) I use the \verb command, not the verbatim environment.

So, when I use something like \verb"HereSomeLongText" no hyphenation is done, so I get my text out of page margins.

How I can get hyphenation inside \verb? Please note that I want inline (inside the same line) verbatim like text.

Here is some minimal (non) working example where you can see the problem described.

\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[left=2cm, right=2.2cm,height=25cm]{geometry}

\DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font=`\-}

\begin{document}
\noindent Here is some TeX example of text  that looks nice, but when 
I Want to type some text like  \verb"myDataFileLongName.csv" using the
verb command, there is no hyphenation nor like break adequately, so the
text inside the verb command get out of the page margins.

\\[0.25cm] So I would appreciate so much any help in order to solve this 
problem. Thanks, thanks,  thanks,  thanks,  thanks,  thanks,  thanks, 
thanks,  thanks,  thanks,  thanks,  thanks,  thanks,  thanks,  thanks,  
thanks,  thanks,  thanks,  thanks.
\end{document}

Best Answer

I can't recommend hyphenating a file name, because this introduces an ambiguity: does the hyphen belong to the name or not?

You may consider using the \path command provided by the url package, by supplementing it with code allowing it to break before capital letters, for instance:

\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[left=2cm, right=2.2cm,height=25cm]{geometry}
\usepackage{url,etoolbox}
\appto\UrlSpecials{%
  \do\F{\penalty0 \mathchar`\F }%
  \do\L{\penalty0 \mathchar`\L }%
  \do\N{\penalty0 \mathchar`\N }%
}
\begin{document}
Here is some TeX example of text  that looks nice, but when
I Want to type some text like  \path{myDataFileLongName.csv} using the
verb command, there is no hyphenation nor like break adequately, so the
text inside the verb command get out of the page margins.

\end{document}

I didn't add all capital letters, do it if you want, or judge from your real cases what's the real need.

enter image description here

The \path command (and also \url) works by cleverly exploiting TeX's math mode features. It would be too long to describe completely how it works; for this application, we can make use of the \UrlSpecials list macro; the characters contained in the list are made (locally) math active and so their appearance triggers substitution with the prescribed replacement text.

With the proposed code, F will become \penalty0 \mathchar`\F (saying \penalty0 F would enter an infinite loop, but \mathchar`\F is safe): the penalty will allow a line break before the F.

If you don't want to globally modify the \path command, you can add to the \UrlSpecials list only locally inside the working of a new command.

\documentclass[a4paper,10pt]{article}
\usepackage[english]{babel}
\usepackage[left=2cm, right=2.2cm,height=25cm]{geometry}
\usepackage{url,etoolbox}

\let\breaktt\path
\patchcmd{\breaktt}
  {\begingroup}
  {\begingroup\apptourlspecials}
  {}{}
\newcommand{\apptourlspecials}{%
  \appto\UrlSpecials{%
    % add to this list
    \do\F{\penalty0 \mathchar`\F }%
    \do\L{\penalty0 \mathchar`\L }%
    \do\N{\penalty0 \mathchar`\N }%
  }%
}
\begin{document}
Here is some TeX example of text  that looks nice, but when
I Want to type some text like  \breaktt{myDataFileLongName.csv} using the
verb command, there is no hyphenation nor like break adequately, so the
text inside the verb command get out of the page margins.

\end{document}

The command \path expands to \leavevmode\begingroup\urlstyle{tt}\Url, so we define \breaktt to do the same but we also add something just after \begingroup, precisely the addition to the list \UrlSpecials we need. In this way \path and \url will behave as before.