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.
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.
The German LaTeX Wikibook contains a chapter about hyphenation -- here's the link.
babel Package
First of all make sure that you use the babel package. It loads hyphenation rules for the language you use.
hyphsubst Package
If you write in German maybe the hyphsubst package is a good option.
Manually Help LaTeX with the Hyphenation (Globally)
As stated here or here or here for example you can add specific hyphenation rules in the preamble of your document:
% needs babel package
\hyphenation{cryp-to-graphy lalala-la lolo-lolo-lulu}
Manually Help LaTeX with the Hyphenation (Locally)
I would recommend this as the last step when you finished your document. Then you can manually help LaTeX.
The command \overfullrule=2pt in the preamble will show you where LaTeX has a problem with hyphenation:
\documentclass{article}
\usepackage[ngerman]{babel}
% showframe to show the layout frames... :)
\usepackage[textwidth=50mm,showframe]{geometry}
% Show overfull boxes and lines (TeX primitive)
\overfullrule=2pt
\begin{document}
Lalalalalalala lalalalala lalalalala lalalalala
\end{document}
Here's a list of manual options:
\-
Hyphenation a word only here and nowhere else (all other hyphenation rules are not valid anymore).
"-
Hyphenation a word also here (the other hyphenation rules are still valid).
"=
This produces a dash. Hyphenation is allowed here and the other hyphenation rules are still valid. Normally if a word contains a dash the dash is the only place where a hyphenation can occur.
"~
This produces a dash where no hyphenation is allowed.
""
Here a hyphenation can occur but no automatic hyphenation dash will be produced by LaTeX.
These commands work if you use ngerman
option for babel
. I do not
know how to make them work if you use other languages. Can somebody help me on this?
\documentclass{article}
\usepackage[ngerman]{babel}
% showframe to show the layout frames... :)
\usepackage[textwidth=50mm,showframe]{geometry}
% Show overfull boxes and lines (TeX primitive)
\overfullrule=2pt
\begin{document}
Lalalalalalala lalalalala lal"-lalala lalalalala
\end{document}
microtype Package
Stolen from the comments (@Henri Menke)!
The microtype package also improves hyphenation. The following cite is taken from the package manual:
Micro-typography is the art of enhancing the appearance and
readability of a document while exhibiting a minimum degree of visual
obtrusion. It is concerned with what happens between or at the margins
of characters, words or lines. Whereas the macro-typographical aspects
of a document (i.e., its layout) are clearly visible even to the
untrained eye, micro-typographical refinements should ideally not even
be recognisable. That is, you may think that a document looks
beautiful, but you might not be able to tell exactly why: good
micro-typographic practice tries to reduce all potential irritations
that might disturb a reader.
Best Answer
Try
\sloppy
(How can I make my text never go over the right margin by always hyphenating or breaking on word-boundaries?).I found that in the Related Links to your question. It worked for me with your text.
To fix just a part of the document:
(from http://ece.uprm.edu/~caceros/latex/introduction.pdf)