3

I would like to format classes, file paths, directory paths, method names and so on in my document. At the very basic each of those should be formatted as \texttt. However, the rules for when LaTeX can line break them are different. For example:

  • Classes: break at '.'. For example "java.util.String" could be broken 2 places
  • File & directory paths: break at '/'. For example "C:\a\b\c" could be broken 3 places
  • Method names: break at spaces. For example "public getText()" could be broken 1 place

How would I go about doing this? The defs below is a starting point, but it doesn't provide any breaking behavior since LaTeX doesn't break \texttt by default.

\newcommand{\class}[1]{\texttt{#1}}
\newcommand{\method}[1]{\texttt{#1}}
..
1

1 Answer 1

8

With the help of the url package we can define three new commands: \class, \filedir, and \method.

First of all we load the package url as follows:

\usepackage[obeyspaces,spaces]{url}

The obeyspaces option is needed otherwise all spaces inside \url-like commands are ignored and the option spaces is needed otherwise we won't be able to break lines at whitespaces.

These are the definitions:

\DeclareUrlCommand\class{%
  \renewcommand{\UrlBigBreaks}{\do\.}%
  \renewcommand{\UrlBreaks}{\do\.}%
  }
\DeclareUrlCommand\filedir{%
  \renewcommand{\UrlBigBreaks}{\do\\}%
  \renewcommand{\UrlBreaks}{\do\\}%
  }
\DeclareUrlCommand\method{%
  \renewcommand{\UrlBigBreaks}{}%
  \renewcommand{\UrlBreaks}{}%
  }

In the first case, we are creating a command that allows breaks only when a . is encountered. In the second one a command that allows breaks only when a \ is encountered. In the third case we leave the list blank since spaces are already allowed by the spaces option at loading time.

MWE:

\documentclass{article}
\usepackage[obeyspaces,spaces]{url}
\DeclareUrlCommand\class{%
  \renewcommand{\UrlBigBreaks}{\do\.}%
  \renewcommand{\UrlBreaks}{\do\.}%
  }
\DeclareUrlCommand\filedir{%
  \renewcommand{\UrlBigBreaks}{\do\\}%
  \renewcommand{\UrlBreaks}{\do\\}%
  }
\DeclareUrlCommand\method{%
  \renewcommand{\UrlBigBreaks}{}%
  \renewcommand{\UrlBreaks}{}%
  }

\begin{document}
text text text text text text text text text text text text text text tex \class{java.util.String}

text text text text text text text text text text text text text text tex \filedir{C:\a\b\c}

text text text text text text text text text text text text text text \method{public getText()}
\end{document} 

Output:

enter image description here

7
  • With the url package is there a way to add e.g. parenthesis to the methods automatically so that \method{hello} becomes "hello()"?
    – johnrl
    Mar 27, 2014 at 16:24
  • @johnrl Yes. In the definition of \method add a line \renewcommand{\UrlRight}{()}% Mar 28, 2014 at 5:50
  • thanks, but that seems to insert two different paren sizes. In particular, the right paren is larger than the left. I solved with \renewcommand{\UrlRight}{$()$}%, but I'm not sure it's the best way.
    – johnrl
    Mar 29, 2014 at 10:42
  • @johnrl I can't reproduce that issue. For me, both parenthesis have same size. Mar 29, 2014 at 10:45
  • Strange. However, it works with the math-mode hack, so I'm happy. Thanks!
    – johnrl
    Mar 29, 2014 at 11:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .