TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Friends :), I would like to have compound words that contain hyphen characters, such as "Baden-Württemberg", to be permitted to be hyphenated at all points in the component words as well (here: "Ba-den" and "Würt-tem-berg").

I know of the discussions that this might look ugly, and I know that I could write something like Ba\-den-Würt\-tem\-berg. But I am providing a package for customer use, and it is hard to make everyone insert escape characters or manual hyphenation instructions on thousands (!) of pages of manuscript. Separation of form and content is, after all, one of the strong features of LaTeX.

So, is there an easy switch to disable the hyphenation prevention for words containing a hyphen? I have only access to the preamble of the document, so I cannot insert anything in the text.

I am using LuaLaTeX and fontspec, so hacking the \hyphenchar like in this post doesn't work. Neither is it an option for me to insert \hyphenation{Ba-den=Würt-tem-berg} into the preamble for each word as was suggested in this post. I have to provide a package for general use.

I don't know if this question might be a duplicate.

Here is a MWE to play around with:

\documentclass{article}
\usepackage{fontspec}

\setmainfont{Latin Modern Roman}[HyphenChar="2010] 
%\setmainfont{Linux Libertine O}[HyphenChar="2010] %does not work
\usepackage[english]{babel}
\usepackage{lipsum}

\begin{document}

\lipsum[1]
Test electromagnetic-endioscopy
electromagnetic-endioscopy
electromagnetic-endioscopy
electromagnetic-endioscopy
Test electromagnetic-endioscopy
electromagnetic-endioscopy
electromagnetic-endioscopy
electromagnetic-endioscopy
electromagnetic-endioscopy
electromagnetic-endioscopy

\end{document}

Note that the HyphenChar="2010 works only for Latin Modern and it prevents hyphenation at the hyphen. That makes it come close to the desired result but hyphenation at the hyphen if possible would be desirable.

share|improve this question
    
If I load the main font as \setmainfont{Latin Modern Roman}[HyphenChar="2010], with XeLaTeX the hyphenation turns out to be Ba-den-Würt-tem-berg (but no line break is taken at the explicit hyphen). This seems to have no effect with LuaLaTeX. – egreg Mar 13 at 16:05
    
What can/should be assumed about the contents of the preamble of your document? E.g., is is OK to assume that babel is going to be loaded? If so, with which language option(s)? – Mico Mar 13 at 16:28
    
@egreg thanks! I included it in the MWE in the question. It would be so much better if hyphenation at the hyphen would also happen if appropriate. – LaTechneuse Mar 13 at 16:34
    
@Mico sure babel is included. English, french, ngerman would be the languages that will be mainly used. – LaTechneuse Mar 13 at 16:35
    
I've taken the liberty of editing your posting a bit to provide more background information on what you're looking to achieve. Hope that's OK with you. Seriously. :-) – Mico Mar 13 at 18:29

(Rewrote the answer to make the solution more like a standalone package.)

It's good news that you're using both LuaLaTeX and babel. :-)

The following proposed solution provides three files

  • A .sty file, to be named comphyph.sty (short for "composite word full hyphenation"...), that loads the babel package, enables the "= shorthand for various languages, and sets up two TeX macros to turn the Lua code on and off

  • A .lua file, to be named comphyph.lua, that contains the Lua code. Is loaded from comphyph.sty.

  • A driver file that demonstrates how to use the code. It loads the comphyph package and runs through various exercises.

Important note: The code works by replacing all instances of <letter>-<letter> with <letter>"=<letter>, where "= is a babel shorthand macro. The code takes care not to perform these replacements inside verbatim, Verbatim, and lstlisting environments. The code also undoes the replacements if they're applied to the arguments of \label, \ref (as well as \eqref, \vref, \autoref, and \cref), \url, \href, \cite, and \bibliography macros. Use the Lua code below as a template to create further exceptions.

The file comphyph.sty:

%% comphyph.sty

\ProvidesPackage{comphyph}[2016/03/15]

%% Load the 'babel' package and enabling babel's '"' 
%% shorthand for languages other than "ngerman". 
%% Augment as needed.

\RequirePackage[ngerman]{babel}

\addto\extrasbritish{\languageshorthands{ngerman}%
                     \useshorthands*{"}}
\addto\extrasenglish{\languageshorthands{ngerman}%
                     \useshorthands*{"}}
\addto\extrasfrench{\languageshorthands{ngerman}%
                    \useshorthands*{"}}


%% Load the file that contains the Lua functions

\directlua{ dofile ( "comphyph.lua" ) }


%% Tex-side macros to enable/disable operation of 
%% "comphyph_ok" Lua function

\newcommand\comphyphOn{%
     \directlua{luatexbase.add_to_callback (
     "process_input_buffer",  comphyph_ok, "comphyph_ok" )}}

\newcommand\comphyphOff{%
     \directlua{luatexbase.remove_from_callback (
     "process_input_buffer" , "comphyph_ok" )}}

\AtBeginDocument{\comphyphOn} % set to "on" by default

The file comphyph.lua:

-- comphyph.lua, 2016/03/16


-- Start by setting up a Boolean variable and
-- setting its default value to "false"

in_verbatim_or_listings = false

-- Do string substitution *except* if we're in a 
-- verbatim, Verbatim, or lstlistings environment

function comphyph_ok ( buff )
   if string.find ( buff , "\\begin{[vV]erbatim}" ) or
      string.find ( buff , "\\begin{lstlisting}" ) then
         in_verbatim_or_listings = true
         return buff
   elseif string.find ( buff , "\\end{[vV]erbatim}" ) or
      string.find ( buff , "\\end{lstlisting}" )then
         in_verbatim_or_listings = false
         return buff
   elseif in_verbatim_or_listings == false then
         buff = string.gsub (buff, '(%a)%-(%a)', '%1"=%2') 
         buff = undo_quote_equal ( buff )
         return buff
   end
end

-- Auxilliary function to undo string substitutions 
-- for text strings in arguments of macros such as
-- \label, \ref, \cite, \url, and \href

function undo_quote_equal ( s )
   s = string.gsub ( s , "(\\label%*?)(%b{})", 
           function ( macro, arg ) 
              arg = string.gsub ( arg , '"=', '-' )
              return macro..arg
           end)
   s = string.gsub ( s , "(\\%a*ref%*?)(%b{})", 
           function ( macro, arg ) 
              arg = string.gsub ( arg , '"=', '-' )
              return macro..arg
           end)
   s = string.gsub ( s , "(\\.-cite.-)(%b{})", 
           function ( macro, arg ) 
              arg = string.gsub ( arg , '"=', '-' )
              return macro..arg
           end)
   s = string.gsub ( s , "(%w%}?%}?%)?%])(%b{})", 
           function ( macro, arg ) 
              arg = string.gsub ( arg , '"=', '-' )
              return macro..arg
           end)
   s = string.gsub ( s , "(\\.-bib.-)(%b{})", 
           function ( macro, arg ) 
              arg = string.gsub ( arg , '"=', '-' )
              return macro..arg
           end)
   s = string.gsub ( s , "(\\url%*?)(%b{})", 
           function ( macro, arg ) 
              arg = string.gsub ( arg , '"=', '-' )
              return macro..arg
           end)
   s = string.gsub ( s , "(\\href%*?)(%b{})(%b{})", 
           function ( macro, arga, argb ) 
              arga = string.gsub ( arga , '"=', '-' )
              argb = string.gsub ( argb , '"=', '-' )
              return macro..arga..argb
           end)
   return s
end 

A "driver" file that illustrates the use of the TeX and Lua code:

% !TEX TS-program = lualatex
%% Demo file for comphyph package

\documentclass{article}

% Create a dummy bib entry
\usepackage{filecontents}
\begin{filecontents}{my-bib.bib}
@misc{ x-y-z , author = "Author", title = "Title", year = 3001}
\end{filecontents}


%% Use either BibTeX/natbib or biber/biblatex 
%\usepackage[round]{natbib}
%\bibliographystyle{plainnat}

\usepackage[backend=biber]{biblatex}
\addbibresource{my-bib.bib}

\usepackage{url,amsmath}
\usepackage{varioref}
\usepackage[colorlinks,allcolors=blue]{hyperref}
\usepackage[nameinlink,noabbrev,capitalize]{cleveref}

\usepackage{fontspec}
\setmainfont{Latin Modern Roman}

\usepackage[british,english,french,ngerman]{babel}

\usepackage{comphyph} %% load the 'comphyph' package

\setlength\parindent{0pt}  % just for this demo file

\begin{document}
%% Verify that hyphenation of hyphenated words is on

\selectlanguage{english}
\parbox{1mm}{extra-terrestrial}

\smallskip
\selectlanguage{ngerman}
\parbox{1mm}{Baden-Württemberg}

\smallskip
\selectlanguage{french}
\parbox{1mm}{laissez-passer}

\bigskip
\selectlanguage{english}

%% Set up a few cross-references

Dummy equation:
\begin{equation} \label{eq-pyth}
1+1=2
\end{equation}

Various cross-references: \ref{eq-pyth}, \eqref{eq-pyth}, \vref{eq-pyth}, \autoref{eq-pyth}, \cref{eq-pyth}.


%% Finally, create a citation call-out and generate the bibliography

\bigskip
A citation call-out: \cite{x-y-z}

%\bibliography{my-bib}  % if using BibTeX/natbib
\printbibliography

\end{document}

Screenshot of output produced by the preceding code:

enter image description here

share|improve this answer
    
By the way, I kept the word "endioscopy" because that's the way you spelled it in your posting. I believe the spelling "endoscopy" may be more prevalent. – Mico Mar 13 at 18:39
    
Woooow thanks a lot! I'm trying this out now. Haha funny, I was copy pasting the "endioscopy" from this post: tex.stackexchange.com/questions/2706/adequate-hyphenation-of-words-already-conta‌​ining-a-hyphen – LaTechneuse Mar 13 at 19:53
    
First, it works and thats amazing (yet another reason to use LuaTeX). There's one further caveat I believe: If a dash is used in a cite key, e.g. \cite{Fank-Sinatra2003}, biblatex will be searching for something like Frank"=Sinatra2003 as a key in the database. Is there a way to work around this? – LaTechneuse Mar 13 at 20:04
    
@LaTechneuse - Arrgh. Fixing this properly is going to take some more work: I realize now that the arguments of certain macros -- \cite, \url, and \href, to name but three -- should be exempted from being processed by multihyph_ok, while it's OK to process the arguments of other macros, such as \emph and \textbf. Give me some time to think about how to address this situation. – Mico Mar 13 at 20:19
    
...and lstlistings too, if needed... I see it very difficult to find all the cases. Nevertheless, it's a good idea... and shows a very powerful option of LuaLaTeX. – Rmano Mar 13 at 21:07

Use the german shortcuts of babel: Baden"=Würtemberg allows hyphenation at all possible breakpoints.

\documentclass{article}
\usepackage{fontspec}
\usepackage{microtype}
\usepackage[ngerman,english]{babel}% english is main language
\useshorthands{"}
\addto\extrasenglish{\languageshorthands{ngerman}}

\usepackage{lipsum}

\begin{document}

    \lipsum[1]
    \begin{sloppypar}
    Test electromagnetic"=endioscopy
    electromagnetic"=endioscopy
    electromagnetic"=endioscopy
    electromagnetic"=endioscopy
    Testtttt Test electromagnetic"=endioscopy
    electromagnetic"=endioscopy
    electromagnetic"=endioscopy
    electromagnetic"=endioscopy
    electromagnetic"=endioscopy
    electromagnetic"=endioscopy
    \end{sloppypar}

\end{document}

enter image description here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.