12

Based on my old question Push long words in a new line, I search for a possibility to allow linebreak on specific character.

For example in my case I want to break on "_" (underscore).

\documentclass{scrartcl} \begin{document} \section{Test} Finally there is a simple solution using \textsc{\textbf{XMLResource.OPTION\_RECORD\_UNKNOWN\_FEATURE}} option. And the text must go on \ldots. \par \end{document} 
2
  • You can issue an \allowbreak wherever needed, as is proposed in Allow line break, but without inserting a dash. Commented Aug 10, 2012 at 6:50
  • Yeah that is true, but I want a "dynamic" solution for a begin/end block, because I want to create a template. Commented Aug 10, 2012 at 7:23

3 Answers 3

12

I suggest you use the \discretionary command for hyphens.

\documentclass{scrartcl} \let\underscore\_ \renewcommand{\_}{\discretionary{\underscore}{}{\underscore}} \begin{document} \section{Test} Finally there is a simple solution using \textsc{\textbf{XMLResource.OPTION\_RECORD\_UNKNOWN\_FEATURE}} option. And the text must go on \ldots. \par \end{document} 

Sample output

Use \discretionary{}{\underscore}{\underscore} instead if you wish the underscore to be placed on the next line, or \discretionary{\underscore}{\underscore}{\underscore} to get the character both before and after the line break.

\discretionary is the hook in to TeX's hyphenation scheme. In some languages, including German, words can change spelling when they are hyphenated, and this command was introduced by Knuth to help cover such situations. \discretionary{a}{b}{c} prints c if there is no linebreak, otherwise it prints a before the linebreak and b after.

If you want to limit this to one particular type of phrase then I suggest you use

\newcommand{\resource}[1]{\textbf{\let\underscore\_ \renewcommand{\_}{\discretionary{\underscore}{}{\underscore}} #1}} 

and write \resource{XMLResource.OPTION\_RECORD\_UNKNOWN\_FEATURE} in your text. (I have removed the \textsc from your example as it had no effect.)

If you wish to use it in one section of the document, then you can similarly define an environment which inserts these definitions at the start, e.g.

\newenvironment{underscoresplit}{\let\underscore\_ \renewcommand{\_}{\discretionary{\underscore}{}{\underscore}}}{} 

used as

\begin{underscoresplit} Finally there is a simple solution using \textsc{\textbf{XMLResource.OPTION\_RECORD\_UNKNOWN\_FEATURE}} option. And the text must go on \ldots. \end{underscoresplit} 
2
  • Thanks, is there any possibility to use it only in a begin/end block? Commented Aug 10, 2012 at 7:21
  • I have extended my answer to address this. Commented Aug 10, 2012 at 7:32
6

You could define your own underscore macro, or redefine the original one, inserting an \allowbreak before or after:

\newcommand{\origunderscore}{} \let\origunderscore\_ \renewcommand{\_}{\allowbreak\origunderscore} 

You could omit the first line if you want, it's just to be safe.

  • At first, we define a macro to reserve the name and so to get an error, if any package already defined it - to avoid accidentally redefining it

  • We store the original underscore macro

  • We redefine the underscore using the original macro, but inserting a possible breakpoint before

While this solves the case of your example, allowing breaks on an arbitrary character is more difficult. Options:

  • You could make this character active and make a macro for it
  • You could use XeTeX and \XeTeXinterchartoks
  • You could use LuaTeX and pre_linebreak_filter

On LaTeX-Community.org we had a very similiar question, just the other way round: Don't split words on certain letters.

2
  • Thanks, is there any possibility to use it only in a begin/end block? Commented Aug 10, 2012 at 7:22
  • 1
    @CSchulz Sure! The first lines could still go to the preamble. The \renewcommand has effect just within the environment, if you place it there. Commented Aug 10, 2012 at 7:29
3

you should use the package url with the command of the same name for such cases. Then it is broken by default at the _:

\documentclass{scrartcl} \usepackage{url,lipsum} \begin{document} \section{Test} \lipsum[5] \begin{sloppypar} Finally there is a simple solution using \url{XMLResource.OPTION_RECORD_UNKNOWN_FEATURE} option. And the text must go on \ldots. \end{sloppypar} \end{document} 
3
  • It would not the best way to show constants as URL, but thanks. Commented Aug 10, 2012 at 7:17
  • you can define your own "URL" command which uses only the same line breaking but another font, eg small caps: \DeclareUrlCommand\myCommand{\urlstyle{sc}} Commented Aug 10, 2012 at 7:24
  • 1
    @Herbert: \urlstyle{sc} doesn't work, you need to use \DeclareUrlCommand\XML{\urlstyle{rm}\scshape} Commented Aug 10, 2012 at 16:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.