# Allow line break, but without inserting a dash

I have a sentence that contains a number of long dataset names. I would like to allow LaTeX to break these names at certain positions that I would like to specify somehow just like I can use \- to allow a word break with hyphenation. But: I would like LaTeX not to insert a dash (which could be mistaken as belonging to the dataset name). How can I do this?

(Something similar is realized in the url package so I guess I could just read that code :) I would not like to use the url package here to be able to make a syntactical (and typographic) difference between urls and datset names.)

-
This may also be of interest: Replace hyphenation character by a backwards arrow – Werner Aug 20 '11 at 16:43

## 7 Answers

You could insert \allowbreak whereever a break without hyphen shall be allowed, such as

long\allowbreak word

-
Is there a way to allow line breaks only at the specified positions? Otherwise LaTeX will often prefer to still add hyphens and break at other positions. (This happens When using e.g. hyphenat for automatic hyphenation.) – fuenfundachtzig Aug 23 '11 at 17:36
You can use \usepackage[none]{hyphenat} together with \allowbreak. – Stefan Kottwitz Aug 23 '11 at 17:44

In addition to Stefan’s answer:

You can also write long""word, but this is a babel-shortcut (as most of the following). As Herbert pointed out these shortcuts are language specific. The list is taken form a german home page and are in the german section of babels manual.

- hyphen sign, no others in this word (hy-phenation > hy-|phenation)
"= hypen sign that allows other breaks (h"=yphenation > h-|y|phen|a|tion)
"~ hyphen sign without line break (hy"~phenation > hy-phenation)
\- possible hyphenation with sign (h\-yphenation > h|y|phen|a|tion)
"" possible hyphenation without sign ((super"~)""hyphenation > (super-)*hy|pen|a|tion)
"| break ligature and allow hyphenation

[( code > output); the pipe indicates possible line break with hyphen sign; asterisk indicates possible line break without sign.]

Source: This german homepage.

-
Seems to be babel specific, yes. – fuenfundachtzig Aug 21 '11 at 12:23
Thanks @fuenfundachtzig I edited my post … – Tobi Aug 22 '11 at 8:43
@Tobi: not only babel specific, also language specific – Herbert Aug 22 '11 at 8:51
@Herbert: Oh you’re right. I added this to my post. – Tobi Aug 22 '11 at 9:08
As far as I can tell. "~ forbids hyphenation in the rest of the word. – Raphael Oct 8 '13 at 16:18

it is also possible to allow hyphenation on Characters

\documentclass{article}
\usepackage[hyphens]{url}
\textwidth=10cm% only for demo
\DeclareUrlCommand\Code{\urlstyle{rm}}
\expandafter\def\expandafter\UrlBreaks\expandafter{\UrlBreaks
\do\/\do\a\do\b\do\c\do\d\do\e\do\f\do\g\do\h\do\i\do\j\do\k
\do\l\do\m\do\n\do\o\do\p\do\q\do\r\do\s\do\t\do\u\do\v
\do\w\do\x\do\y\do\z
\do\A\do\B\do\C\do\D\do\E\do\F\do\G\do\H\do\I\do\J\do\K
\do\L\do\M\do\N\do\O\do\P\do\Q\do\R\do\S\do\T\do\U\do\V
\do\W\do\X\do\Y\do\Z}
\begin{document}
\noindent\rule{\textwidth}{1pt}
some very long text before the hyphenated code
\Code{someverylongtextbeforethehyphenatedcode}

some very long text before the hyphenated code
\Code{some-long-command-name-with-a-lot-of-dases}
\end{document}

-
Very nice, because it's automatic and does not add hyphens. But what if I have e.g. longtextthatdoesnothavecharactersurlbreaksatatanypositionbutneedstobebroken -- I'd need to indicate how (where) to break this, but the \Code enviroment will print any commands in verbatim mode... – fuenfundachtzig Aug 21 '11 at 12:40
see edited answer – Herbert Aug 21 '11 at 13:31
OK, that's certainly one solution. Although I had hoped for something more along the lines of \allowbreak or \-... Because like this it's still not possible to specify where exactly LaTeX should break the line. – fuenfundachtzig Aug 23 '11 at 12:31

From The TeXbook:

\discretionary{<pre-break text>}{<post-break text>}{<no-break text>}


You could try something like:

\newcommand{\sep}{\discretionary{}{}{}}
\newcommand{\datasetname}{My\sep long\sep data\sep set\sep name}

-

@fuenfundachzig: Since you're familiar with the url package, you may want to try the following: Include the instructions \usepackage{url} and \urlstyle={same} in the preamble (instructing the url package to use the main text font for URLs), and then enclose the words that you'd like to be allow broken up inside \url{...} commands. In my experience, this works very well most of the time, but it's not 100% foolproof (narrensicher?), naturally.

-
Oops, just noticed that this answer is a near-perfect duplicate of the one posted by Herbert! Sorry. – Mico Aug 20 '11 at 17:00
You don't need to write @fuenfundachtzig here, as the question asker, he or she will automatically be notified of your answer, just like you as the answerer ("post owner") will be notified of this comment of mine without me using the @.... I don't think it's a problem that your answer is very similar to Herbert's. You could edit your answer to highlight what's different (and maybe better) about your solution. If the differences are really tiny, you could delete your answer and add a comment to Herbert's answer. And finally, foolproof is a good translation of narrensicher :). – doncherry Aug 20 '11 at 18:03

I've had good results with

\linebreak[X]


Where X can be anything from 0 to 4. Zero means "if you really want, you can break the line here", while four means "break the line here, always".

To allow for long word breaking, you can insert it like this:

verylongword\linebreak[0]{}thatshouldbebroken


I'm not sure if it does not require specific packages though.

-

A possibly “gentle” solution is to input your long words with spaces where the line breaks are allowed. The separator is customizable, here I added an optional argument for changing it at will.

I also disabled hyphenation by setting a language with no patterns; this is not necessary if a monospaced font is uses, as usually monospaced fonts disable hyphenation.

\documentclass{article}
\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\Code}{ O{~} m }
{
\group_begin:
\tl_set:Nn \l_tmpa_tl { #2 }
\tl_replace_all:Nnn \l_tmpa_tl { #1 } { \allowbreak }
\language\csname l@nohyphenation\endcsname % or \language255
\tl_use:N \l_tmpa_tl
\group_end:
}
\ExplSyntaxOff

\begin{document}
\parbox{10cm}{
some very long text before the hyphenated code
\Code{some very long text before thehyphenated code}

some very long text before the hyphenated code
\Code[-]{some-long-command-name-with-a-lot-of-dashes}
}

\bigskip

\parbox{3cm}{
\Code{some very long text before thehyphenated code}
}
\end{document}


-