4

I’m trying to use the Unicode ellipsis character (U+2026 ‘…’) in my source files. To this end, I’m looking for an ellipsis macro which I can use with newunicodechar.

The trouble is, I’ve noticed, that the spacefactor is not set correctly around ellipses. This makes solutions like those suggested at Ellipsis with a period/fullstop not work too well.

To illustrate (magnify the results):

\documentclass{article}
\newcommand*{\egap}{\kern\fontdimen3\font}  % Taken from the LaTeX definition
\begin{document}
dots:   \\
I.\egap.\egap.          I   \quad(bad)  \\
I.\egap.\egap.\@        I   \quad(good) \\
I.\egap.\egap.\egap.    I   \quad(good) \\
I.\egap.\egap.\egap.\@  I   \quad(bad)
\end{document}

The ellipsis package fixes some issues with uneven spacing, but does not correctly set the space factor. To illustrate:

\documentclass{article}
\usepackage[xspace]{ellipsis}
\newcommand*{\egap}{\kern\fontdimen3\font}  % For comparison
\begin{document}
[ellipsis]:   \\
I.\egap.\egap.\@        I   \quad(good, for comparison) \\
I\textellipsis          I   \quad(bad)      \\
I\textellipsis\@        I   \quad(terrible) \\
I\textellipsis.         I   \quad(good)     \\
I\textellipsis.\@       I   \quad(bad)
\end{document}

How can I get the spacing correct?

2 Answers 2

3

For restricted cases, \@ifnextchar is sufficient:

\documentclass{article}
\usepackage[utf8]{inputenc}

\newcommand*{\egap}{\kern\fontdimen3\font}
\newcommand*{\wordspace}{\@\space}
\makeatletter\newcommand*{\elip}{.\egap.\egap.\@ifnextchar.\egap\wordspace}\makeatother
\usepackage{newunicodechar}
\newunicodechar{…}{\elip}

\begin{document}
macro:                  \\
I\elip  I   \quad(good) \\
I\elip. I   \quad(good)

Unicode glyph:          \\
I…      I   \quad(good) \\
I….     I   \quad(good)

But—                    \\
I\elip, I   \quad(bad)  \\
I\elip! I   \quad(bad)  \\
I\elip? I   \quad(bad)

\end{document}

A fuller version requires a generalization of \@ifnextchar. Adapting egreg’s answer to a related question leads toward such a generalization:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand{\elip}{ } { \elip_main: }
\NewDocumentCommand{\addtoelipexceptions}{m}
{
    \tl_gput_right:Nn \g_elip_exceptions_tl { #1 }
}
\cs_new_protected:Npn \elip_gap
{
    \kern\fontdimen3\font
}
\cs_new_protected:Npn \elip_main:
{
    .\elip_gap.\elip_gap.
    \bool_set_true:N \l_elip_apply_bool
    \peek_catcode_ignore_spaces:NF \c_space_token { \elip_check: }
}
\cs_new_protected:Npn \elip_check:
{
    \tl_map_inline:Nn \g_elip_exceptions_tl
        {
            \token_if_eq_charcode:NNT ##1 \l_peek_token
                {\bool_set_false:N \l_elip_apply_bool \prg_map_break: }
        }
    \bool_if:NTF \l_elip_apply_bool
        { \@~ }
        { \elip_gap }
}
\tl_new:N \g_elip_exceptions_tl
\ExplSyntaxOff

\usepackage{newunicodechar}
\newunicodechar{…}{\elip}

\begin{document}
macro:                  \\
I\elip  I   \quad(good) \\
I\elip. I   \quad(good)

Unicode glyph:          \\
I…      I   \quad(good) \\
I….     I   \quad(good)

But—                    \\
I…,     I   \quad(bad)  \\
I…!     I   \quad(bad)  \\
I…?     I   \quad(bad)

\addtoelipexceptions{,.!?}
Now:                    \\
I…,     I   \quad(good) \\
I…!     I   \quad(good) \\
I…?     I   \quad(good)
\end{document}    

(Improvements to expl3 style, or an actual generalized \@ifnextchar, would be welcome.)

1
  • Yes, nesting \@ifnextchar invocations will also work, but loops are better & more configurable. Jul 4, 2012 at 1:50
1

Returning to this question…

The ellipses package exposes a hook:

\RequirePackage{ellipsis}
\renewcommand{\ellipsis@after}{\@}
\newunicodechar{…}{\textellipsis}

does the trick.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .