Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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}
share|improve this question
    
Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. –  egreg Sep 29 '13 at 20:27
2  
@Mico Well, with the standard setup, LaTeX doesn't hyphenate words in typewriter type: \DeclareFontFamily{OT1}{cmtt}{\hyphenchar \font\m@ne}. See tex.stackexchange.com/questions/44361/… –  egreg Sep 29 '13 at 20:30
    
@egreg I edited my post in order to provide a minimal working example. I read the submitted link, but I cant find the solution to my problem. Thanks. –  user37476 Sep 29 '13 at 23:22
    
Hyphenating a file name is not really a good thing: the reader won't know for sure if the hyphen is part of the name or not. Consider the \path command of the url package. –  egreg Sep 29 '13 at 23:26
    
@egreg First all, thanks for your help. I appreciate it so much. The file name was only an (bad) example of the unexpected behavior in \verb. –  user37476 Sep 30 '13 at 1:19

1 Answer 1

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.

share|improve this answer
    
Dear @egreg. Your solution works fine for my purposes, and that is great for me. I would like to know how your solution works, in order to get into a deep knowledge of LaTeX. Thanks. –  user37476 Sep 30 '13 at 1:22

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.