3

Does anyone know how to use the hyphenat package in pdflatex to create an automatic hyphenation rule whereby forwards slashes may be used to break words over lines, or as hyphenation points? I've read the documentation for the hyphenat package, but I can't seem to find a way to create a global rule.

Ideally, I'd like to be able to allow line breaks for words with forward slashes in, e.g. to change

Lisinopril/Hydrochlorothiazide

to

Lisinopril/
Hydrochlorothiazide

This is just one example of this. I have many drug class combinations like the above in tables in my appendices, and I need some automated way to prevent line overflows.

Feakster
  • 131
  • 1
    you can use \slash instead of / – David Carlisle Apr 03 '19 at 10:38
  • The danish babel definitions contains the following {\bbl@allowhyphens\discretionary{/}{}{/}\bbl@allowhyphens}{}}, this means under danish babel I can use "/ to enable line breaking at a /, you might be able to adapt this to what ever language you are using. (note that under danish babel, " is an active char) – daleif Apr 03 '19 at 10:41

1 Answers1

3

The two approaches are to either make / look like a letter as far as the hyphenation algorithm goes, or else to reset the hyphenation algorithm following a slash.

(1)

A direct steal/modification of my answer here: Add hyphenation pattern for word with special characters

Of course, it helps to define the slashed words hyphenation patterns in advance.

\documentclass{article}
\lccode`\/`\/

\hyphenation{Lis-in-o-pril-/Hy-dro-chlor-o-thi-a-zide}

\setlength\textwidth{1mm} % just for this example
\begin{document}
. Lisinopril/Hydrochlorothiazide
\end{document}

enter image description here

Without manually specifying the hyphenation pattern in advance, it is still hyphenatable, but just not optimal:

enter image description here

(2)

The other way is to follow the / with \hspace{0pt}, which I codify here as \?:

\documentclass{article}
\newcommand\?{/\hspace{0pt}}
\setlength\textwidth{1mm} % just for this example
\begin{document}
. Lisinopril\?Hydrochlorothiazide
\end{document}

enter image description here

This approach works because the \hspace{0pt} resets the hyphenation algorithm to start looking again for new places to hyphenate.

  • Thank you for your reply. Is there any way to make "/" a possible break point without altering all the "/"s to something else in the document. Currently, I'm compiling my pdfs using Sweave in RStudio, and the "/"s all occur in tables I'm reading into my main document in R code chunks, .e.g. – Feakster Apr 03 '19 at 11:10
  • @Feakster Approach (1) does that. The key is to use \lccode\/`/` in the preamble, and ideally to specify hyphenation patterns for such words. – Steven B. Segletes Apr 03 '19 at 11:12
  • @Feakster I am sorry that I am not a user of R and cannot offer assistence on that end. – Steven B. Segletes Apr 03 '19 at 11:14
  • Sorry, I'm struggling to format my code for the comments boxes. – Feakster Apr 03 '19 at 11:18
  • @Feakster Rather than a comment, it might be better to edit your question, showing this extra code in a supplement to the original question. – Steven B. Segletes Apr 03 '19 at 11:19
  • Instead of \? one can use \slash from the LaTeX kernel. – ljrk Apr 13 '21 at 14:37
  • @ljrk You could use \slash instead of / inside the definition of \?. However, just typing Lisinopril\slash Hydrochlorothiazide will make the "Hydro..." portion un-hyphenatable. – Steven B. Segletes Apr 13 '21 at 14:46
  • @StevenB.Segletes shouldn't \slash reset the hyphenation algorithm as well? – ljrk Apr 13 '21 at 15:54
  • @ljrk I am quite unfamiliar with what it should do. However, I substitute it into my MWE and observe what it does do. – Steven B. Segletes Apr 13 '21 at 15:57
  • @StevenB.Segletes true, I dug a little and this answer by egreg sheds some light onto the issue: https://tex.stackexchange.com/a/121959/90407. By default, \slash acts like a hyphen (thus disallowing hyphenation afterwards). – ljrk Apr 14 '21 at 17:21
  • @ljrk Thank you for the informative link. – Steven B. Segletes Apr 14 '21 at 17:26