0

How might one define a lualatex macro \lip, taking optional arguments * and [N], such that, with no argument, the expansion is an ellipsis consisting of three dots, with [N] it has N dots, and with * its ends a sentence? It should be possible to use it like \lip this, ie. without closing it with {}.

PS. Ideally, if the ellipsis is followed by a comma, the space between the last dot and the comma should be the same as the space between the dots.

  • 1
    What do you mean by “end a sentence”? – egreg Jan 18 at 18:44
  • @egreg, spacefactor 3000. – Toothrot Jan 18 at 18:45
4

You do not need LuaLaTeX for this, it can be done quite easily with expl3:

enter image description here

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{expl3,xparse}

\ExplSyntaxOn
\NewDocumentCommand\lip{s O{3}}{
  $
    \prg_replicate:nn{#2}{\ldotp}
  $
  \IfBooleanT{#1}{\spacefactor\sfcode`\.\relax}
}
\ExplSyntaxOff

\begin{document}
Hello \lip, I know how to write dots: \lip*[10] Anyway \lip[3] not all dots end sentences.
\end{document}

Of course, if you use it like some \lips words, TeX will gobble the space. This is very hard to avoid even with LuaTeX because LuaTeX does not change the TeX parsing rules. Of course this problem does not exists if you use a star or the optional argument.

If you really need it, there are three options I can think of:

  • Always add the space if no argument has been given. This would break e.g. at \lip, in the example above.
  • Use xspace but remember the drawbacks
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{expl3,xparse,xspace}
\ExplSyntaxOn
\NewDocumentCommand\lip{s o}{
  $
    \prg_replicate:nn{\IfValueTF{#2}{#2}{3}}{\ldotp}
  $
  \IfBooleanTF{#1}{
    \spacefactor\sfcode`\.\relax
  }{
    \IfValueF{#2}{\xspace}
  }
}
\ExplSyntaxOff

\begin{document}
Hello \lip, I know how to write dots: \lip*[10] Anyway \lip not all dots end sentences.
\end{document}
  • The "LuaTeX solution": Use a process_input_buffer callback to detect all input lines where \lip is used and always add an explicit space afterwards. This would be extremely fragile and will not interact properly with macros etc.

So it is much more reliable to just manually add \ or [3] when this occurs.

  • You don't need expl3's \prg_replicate:nn or similar in your code, because it is anyhow not purely expandable, so simply using TeX/LaTeX \loop construct with a count can do the job. – user4686 Jan 18 at 20:19
  • @jfbu That would work, but I think \prg_replicate:nn looks much cleaner. – Marcel Krüger Jan 18 at 20:27
  • well, if document uses already expl3, you are certainly right – user4686 Jan 18 at 21:18
  • @jfbu Just loading expl3 adds about 0.5 seconds of startup time to my LaTeX run. I try to avoid loading it where possible. – Henri Menke Jan 18 at 22:42
4

enter image description here

\documentclass{article}
\makeatletter
\def\lip{\@ifstar{\let\@liptmp\relax\@lip}{\let\@liptmp\@\@lip}}
\newcommand\@lip[1][3]{{\uccode`m=`.\uppercase\expandafter{\romannumeral#1000}}\@liptmp\space \ignorespaces}

\begin{document}


aaa\lip[5] bbb aaa\lip bbb aaa\lip[7] bbb

aaa\lip*[5] bbb aaa\lip* bbb aaa\lip*[7] bbb

\end{document}
  • 1
    +1 even without \zz! – user4686 Jan 18 at 21:20
1

I guess that after \lips* you want a normal space (subject to the space factor):

\documentclass{article}
\usepackage{xparse}

% the definition of \textellipsis is
% .\kern\fontdimen3\font
% repeated three times

\ExplSyntaxOn
\NewDocumentCommand{\lips}{sO{3}}
 {
  \prg_replicate:nn { #2 } { .\kern\fontdimen3\font }
  \IfBooleanT{#1}
    { \unkern\spacefactor 3000 \scan_stop: \c_space_tl }
  \ignorespaces
 }
\ExplSyntaxOff

\begin{document}

Three \lips Four \lips[4] Five \lips[5], with a comma

Here is \lips It was not end of sentence.

Here is \lips* It was end of sentence.

Here is \mbox{\lips\unkern} It was not end of sentence

\end{document}

The last line emulates a normal space not subject to the space factor, so to appreciate the difference.

enter image description here

  • looks as if the space before the ellipsis is larger than after. – Toothrot Jan 18 at 22:30
  • @Toothrot It's a normal space. You didn't specify what you want. – egreg Jan 18 at 22:37
  • oh i see what is going on. the space after the ellipsis is unstrechable – Toothrot Jan 18 at 22:45
  • @Toothrot I tried to guess, maybe you can be more precise. – egreg Jan 18 at 22:48

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.