(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:

\setmainfont{Latin Modern Roman}[HyphenChar="2010]
, with XeLaTeX the hyphenation turns out to beBa-den-Würt-tem-berg
(but no line break is taken at the explicit hyphen). This seems to have no effect with LuaLaTeX.babel
is going to be loaded? If so, with which language option(s)?babel
is included. English, french, ngerman would be the languages that will be mainly used.