2

I am using the hyphenat package with the htt option.

The whole thing is used to document an application programming interface (API). Consider the following MWE:

\documentclass{minimal}
\usepackage[english]{babel}
\usepackage[htt]{hyphenat}
\usepackage{hyperref}
\usepackage{xparse}
\usepackage{microtype}
\hyphenation{An-Obscenely-Long-Function-Name}
\NewDocumentCommand{\funcref}{mo}{
    \hyperref[#1]{\ttfamily{#1(\IfValueTF{#2}{#2}{})}}}
\begin{document}
    foo bar baz \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName}
\end{document}

The idea here is to use the custom command \funcref to typeset the function name and refer to a label with that name at the same time. Additionally parentheses (and optionally content inside the parentheses) may be passed as second argument.

Now I have a number of problems with the outcome:

  1. I'd like to have the hyphenation provided by hyphenat with htt option, but without an actual hyphen character (at least for teletype text).
  2. I'd like to prevent hyphenation to kick in between the function name and the parentheses.
  3. Ideally I'd like for LaTeX to be able to figure out the hyphenation based on the capitalization inside the function names. However, this one is not a biggie, I can implement this in Python and feed it to LaTeX in a digestible form instead.

Any ideas?

  • According to this answer you can set \hyphenchar\<currentfont>=-1 to "turn it off". Now you "just" need to figure out the current font when you are using the teletype text. – Stefan Schmiedl Jun 29 at 21:08
  • @StefanSchmiedl ... and just need a way for that to "snap back" after use. – 0xC0000022L Jun 30 at 19:22
1
+100

With luatex you could set \prehyphenchar to zero to suppress the hyphenchar. The setting is by language, so I used ngerman to switch, but you can also define a new language:

\documentclass{article}
\usepackage[ngerman,english]{babel}
\usepackage{xparse}
\usepackage{microtype}

\usepackage{fontspec}
\newfontfamily\myfunctt{lmmono10-regular.otf}

\selectlanguage{ngerman}
\prehyphenchar=0
\hyphenation{An-Obscenely-Long-Function-Name}

\selectlanguage{english}
\prehyphenchar=`\-

\NewDocumentCommand{\funcref}{mo}{{\selectlanguage{ngerman}\myfunctt
    \hyperref[#1]{#1(\IfValueTF{#2}{#2}{})}}}

\usepackage{hyperref}

\begin{document}
AnObscenelyLongFunctionName AnObscenelyLongFunctionName AnObscenelyLongFunctionName
AnObscenelyLongFunctionName AnObscenelyLongFunctionName

    foo bar baz \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName}
\end{document}

enter image description here

With pdflatex you can do something similar (assuming that your func names are only ascii) by using an OT1 ttfont and using a char from the non-ascii range as hyphenchar (you will get lots of missing chars messages in the log):

\documentclass{article}
\usepackage[ngerman,english]{babel}
\usepackage{xparse}
\usepackage{microtype}
\usepackage[T1]{fontenc}
\DeclareRobustCommand\funcfamily
        {\fontencoding{OT1}\fontfamily\ttdefault\selectfont}
\hyphenation{An-Obscenely-Long-Function-Name}


\NewDocumentCommand{\funcref}{mo}{{%
    \funcfamily\hyphenchar\font=130
    \hyperref[#1]{#1(\IfValueTF{#2}{#2}{})}}}

\usepackage{hyperref}

\begin{document}
AnObscenelyLongFunctionName AnObscenelyLongFunctionName AnObscenelyLongFunctionName
AnObscenelyLongFunctionName AnObscenelyLongFunctionName

    foo bar baz \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName} \funcref{AnObscenelyLongFunctionName}
\end{document}
  • Thank you very much. It's not quite automated to the extent I would desire, but I can augment it with Python code and so all is fine. Danke für die Zeit welche Sie in die Beantwortung meiner Frage gesteckt haben. – 0xC0000022L Jul 2 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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