0

I found this answer on http://www.oipapio.com/question-79176

\documentclass[10pt,a5paper,twoside]{article}
\usepackage{hyphenat}

\cs_new:Npn \hyphenfix_emdash:c {---}
\cs_new:Npn \hyphenfix_endash:c {--}

\cs_new:Npn \hyphenfix_discardnext:NN #1#2{#1}


\catcode`\-=\active

\cs_new_protected:Npn -{
    \futurelet\hyphenfix_nexttok\hyphenfix_i:w
}

\cs_new:Npn \hyphenfix_i:w {
    \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
        %discard the next `-` token
        \hyphenfix_discardnext:NN{\futurelet\hyphenfix_nexttok\hyphenfix_ii:w}
    }{
        % from package hyphenat
        \hyp
    }
}

\cs_new:Npn \hyphenfix_ii:w {
    \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
        \hyphenfix_discardnext:NN{\hyphenfix_emdash:c}
    }{
        \hyphenfix_endash:c
    }
}

\begin{document}

\section{Show font}

    Tests.

    Encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding.

    Encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding.

\end{document}

It should allow me to use - as \hyp{} and two -- as one -, but when compiling it throws this error:

Package: hyphenat 2009/09/02 v2.3c hyphenation utilities
\langwohyphens=\language77
LaTeX Info: Redefining \_ on input line 43.
)test1.tex:4: Undefined control sequence
Here is how much of TeX's memory you used:
 232 strings out of 493314

This feature would be very welcomed because, in my language, I use a lot of - (hyphens) and it is horrible to keep typing (and remembering to type) \hyp{} everywhere.

Related questions:

  1. Adequate hyphenation of words already containing a hyphen
  2. Hyphenat not hyphenating words with hyphens
  3. How to copy minus as hyphen?
  4. Problem expanding acronyms containing \hyp{} in captions
  5. Hyphenation problem

Update

I managed to get it working after adding the expl3 package. I thought it was not required. Full example:

\documentclass[10pt,a5paper,twoside]{article}
\usepackage{hyphenat}
\usepackage{expl3}

\ExplSyntaxOn
\cs_new:Npn \hyphenfix_emdash:c {---}
\cs_new:Npn \hyphenfix_endash:c {--}

\cs_new:Npn \hyphenfix_discardnext:NN #1#2{#1}


\catcode`\-=\active

\cs_new_protected:Npn -{
    \futurelet\hyphenfix_nexttok\hyphenfix_i:w
}

\cs_new:Npn \hyphenfix_i:w {
    \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
        %discard the next `-` token
        \hyphenfix_discardnext:NN{\futurelet\hyphenfix_nexttok\hyphenfix_ii:w}
    }{
        % from package hyphenat
        \hyp
    }
}

\cs_new:Npn \hyphenfix_ii:w {
    \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
        \hyphenfix_discardnext:NN{\hyphenfix_emdash:c}
    }{
        \hyphenfix_endash:c
    }
}
\ExplSyntaxOff

\begin{document}

\section{Show font}

    Tests.

    Encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding.

    Encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding\hyp{}encoding.

\end{document}

enter image description here


Now, by curiosity. Why I do not see much people using this trick everywhere else?

Does this command is breaking something in latex? Then, is it unadvised to use it?

user
  • 4,745

1 Answers1

3

In german we use a shortcut provided by babel "= to input such hyphens. You could activate such a shortcut too, see the babel documentation.

I would never in a document activate the hyphen and use code like yours. It can explode it much too many places. Just to show two uncomment the lines marked with "error":

\documentclass[10pt,a5paper,twoside]{article}
\usepackage{hyphenat}
\usepackage[ngerman]{babel}
\usepackage{expl3}
\usepackage{tikz}
\ExplSyntaxOn
\cs_new:Npn \hyphenfix_emdash:c {---}
\cs_new:Npn \hyphenfix_endash:c {--}

\cs_new:Npn \hyphenfix_discardnext:NN #1#2{#1}


\catcode`\-=\active

\cs_new_protected:Npn -{
    \futurelet\hyphenfix_nexttok\hyphenfix_i:w
}

\cs_new:Npn \hyphenfix_i:w {
    \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
        %discard the next `-` token
        \hyphenfix_discardnext:NN{\futurelet\hyphenfix_nexttok\hyphenfix_ii:w}
    }{
        % from package hyphenat
        \hyp
    }
}

\cs_new:Npn \hyphenfix_ii:w {
    \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
        \hyphenfix_discardnext:NN{\hyphenfix_emdash:c}
    }{
        \hyphenfix_endash:c
    }
}
\ExplSyntaxOff

\begin{document}

\section{Show font}

    Tests.

    Encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding.

Encoding"=encoding"=encoding"=encoding"=encoding"=encoding"=encoding"=encoding"=encoding"=encoding.

\newlength\mylength
%\setlength\mylength{-1cm} %error

%\tikz\draw(0,0)--(1,1);   %error

\end{document}
Ulrike Fischer
  • 327,261
  • Thank you for this! Until now I have been using the hyphenat package. But this is much easier :)

    I actually found out yesterday, that there is a \hyphen{} command, which does basically the same as \hyp{} but without needing the hyphenat package. I could not find out which ressource this command is delivered from. Does anybody know?

    – The Shadow Sep 06 '19 at 07:29