133

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.)

8 Answers8

137

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

long\allowbreak word
Stefan Kottwitz
  • 231,401
  • 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
  • 1
    You can use \usepackage[none]{hyphenat} together with \allowbreak. – Stefan Kottwitz Aug 23 '11 at 17:44
  • 2
    Also extremely useful if one has a text-segment that goes like WORD/WORD, where the ideal breaking point would be »/« – Kubo Jan 21 '22 at 16:07
  • This worked great for my use case: \texttt{sysctl net.\allowbreak ipv6.\allowbreak conf.\allowbreak interface.\allowbreak accept\_dad\allowbreak =\allowbreak 0}. – William John Holden Jan 04 '24 at 17:05
37

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.

Fran
  • 80,769
Tobi
  • 56,353
13

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 –  Aug 21 '11 at 13:31
  • 1
    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
12

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.

7

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}
Matteo Gamboz
  • 432
  • 3
  • 8
6

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}

enter image description here

egreg
  • 1,121,712
6

@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.

Mico
  • 506,678
  • 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
  • 2
    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
1

Use pakage seqsplit, in main text: \seqsplit{qervnjksnvdjsn}, this means it can linebreak between every singal character, without dash or hyphen sign. Sorry for bad English. :)

dylan
  • 11