2

at the risk that this has been solved somewhere, but I just can't find a solution: I want to highlight java class names in my text, for what I've created a macro \newcommand{\class}[1]{\textcolor{Green4}{\texttt{#1}}}. But when I use e.g. \class{ALongClassName}, and the text is longer than the rest of the line, the text protrudes out of the regular paragraph. I'd prefer an automatical linebreak before the word when it's too long to fit in the line it's currently in.

I've tried \-\class{ALongClassName}, but in case of breaking it displays a - character at the end of the line. Are there any ways of preventing this or possible better methods to accomplish what I need?

Any ideas are greatly appreciated, thanks in advance.

2 Answers 2

3

A variation on \filbreak (The TeXbook, page 111):

\documentclass{article}
\usepackage{xcolor}
\usepackage{showframe}

\newcommand{\class}[1]{%
  \hfil\penalty0 \hfilneg
  \textcolor{red}{\texttt{#1}}%
}

\begin{document}
Here is an example of long text given to class
\class{SomeVeryLongClassNameThatNeedsToBePushedToTheNextLine}
and text after it and a \class{ShortClassName}

enter image description here

Here's a different suggestion: breaking at uppercase letters. This assumes that class names are only made by ASCII letters.

\documentclass{article}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{showframe}

\ExplSyntaxOn
\NewDocumentCommand{\class}{m}
 {
  \textcolor{red}{\texttt{\lukelr_split_class:n { #1 }}}
 }
\cs_new_protected:Nn \lukelr_split_class:n
 {
  \tl_map_inline:nn { #1 }
   {
    \int_compare:nT { \char_value_uccode:n { `##1 } =`##1 }
     { \discretionary{}{}{} }
    ##1
   }
 }
\ExplSyntaxOff

\begin{document}
Here is an example of long text given to class
\class{SomeVeryLongClassNameThatNeedsToBePushedToTheNextLine}
and text after it and a \class{ShortClassName}

\end{document}

The code compares each character with its uppercase counterpart; if they are equal, an empty discretionary is added before the character, making a line break feasible.

enter image description here

1

I think the problem is related to what is described here: How to automatically hyphenate within \texttt?. Applying that solution to your class macro yields the desired results:

enter image description here

Notes:

  • The showframe package was used just to show the page margins. It is not needed in your actual use case.

Code:

\documentclass{article}
\usepackage{xcolor}
\usepackage{showframe}

\newcommand*{\EnableHyphenationInTexttt}{\hyphenchar\font=45\relax}% breakable \texttt


\newcommand{\classOld}[1]{\textcolor{red}{\texttt{#1}}}
\newcommand{\class}[1]{\textcolor{red}{\texttt{\EnableHyphenationInTexttt#1}}}


\begin{document}
Here is an example of long text given to class: \classOld{SomeVeryLongClassNameThatNeedsToWrapAcrossMultipleLines}

\medskip
Here is an example of long text given to class: \class{SomeVeryLongClassNameThatNeedsToWrapAcrossMultipleLines}

\end{document}
1
  • Hey, thanks for your answer! But is there a way to wrap the entire \texttt-text to the next line, not just the part that doesn't fit? Like placing the line break in front of the \texttt?
    – LukeLR
    Jun 2, 2016 at 20:02

You must log in to answer this question.

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