[Tex/LaTex] Line overflow with long filenames and urls

line-breakingurls

Consider the following text. Here /etc/postgresql/8.4/main/pg_hba.confoverflows into the margin, and is hypenated. /var/lib/pgsql/data/pg_hba.conf is also hyphenated, but does not overflow.

So, I was wondering what was a good general way to deal with the overflow. I've run into this issue before, and usually I just stick a // before the offending text, but this is not a good general solution. For example, if the text changes, then the line break may be for no good reason.

I'm actually surprised that LaTeX doesn't simply move that text onto the next line. Is there some good general way to say – don't break this text and move it to the next line if it overruns? I've looked at other questions, and it is possible no such thing exists, but maybe I'm missing something.

\documentclass{article}
\usepackage{underscore}
% Text layout
\topmargin 0.0cm
\oddsidemargin 0.5cm
\evensidemargin 0.5cm
\textwidth 16cm
\textheight 21cm 
\newcommand{\sourcefile}[1]{\texttt{#1}}

\begin{document}

Therefore, the user running init_db.py needs to have sudo
privileges on the machine, or have a database superuser of the same
name as the shell account. In addition, you may need to modify the
PostgreSQL client authentication configuration. This is located in the
file pg_hba.conf. I give basic instructions about the
necessary modifications in this section. For further information about
PostgreSQL client authentication, see
http://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html

With the Debian and Ubuntu default client authentication configuration
located at \sourcefile{/etc/postgresql/8.4/main/pg_hba.conf}, SNPpy
will run without modification. For other Linux distributions, changes
may be necessary.

For example, the client authentication configuration file is located
in Fedora at \sourcefile{/var/lib/pgsql/data/pg_hba.conf} and its
default values are given in Table 1.

\end{document}

Best Answer

You can use the url package:

\usepackage{url}
\newcommand{\sourcefile}{\url}

Using the above, and applying \url{} to the URL yields:

enter image description here


Alternatively, you could use the `hyperref package:

\usepackage{hyperref}
\newcommand{\sourcefile}{\url}

and get click-able links:

enter image description here

If the document is strictly for online viewing you could also use

\href{http://www.postgresql.org/docs/current/static/auth-pg-hba-conf.html}{auth pg hba conf}

and get links with text that does not show the entire URL but whatever text you specify as the second parameter.

enter image description here


As egreg points out, if you don't want click able links on the files, you could use \path{} for the \sourcefiles and use \url for the web links

\usepackage{hyperref}
\newcommand{\sourcefile}{\path}

enter image description here

With the url package, you could also define different macros, and each can have it own style:

\DeclareUrlCommand\email{\urlstyle{rm}} 
\DeclareUrlCommand\directory{\urlstyle{tt}}

Note:

  • The showframe package was used to display the margins.

  • As Martin Scharrer pointed out is is best to use

    \newcommand{\sourcefile}{\path}%  Better!!
    

    or

    \let\sourcefile\path%  Better!!
    

    instead of

    \newcommand{\sourcefile}[1]{\path{#1}}% Not so good
    

    This will allow for the case where there any special characters in the file names as they will be processed in verbatim mode. So, for example, the following will work fine with the either of the "Better" labeled definitions above:

    \sourcefile{~/.cshrc}
    \sourcefile{abc%20def}