5

I am working on an index of first lines for a poetry book and would like the punctuation of the lines NOT to affect the order in which they appear in the index. For example:

Now entries appear in the following order (I think because comma comes before semicolon):

Winter has fled, grass..., 234

Winter has fled; already..., 178

And I would like them to appear in the following order:

Winter has fled; already..., 178

Winter has fled, grass..., 234

When I run the following I get the first option, and I would like to get the second.

\documentclass[10pt]{octavo}
\usepackage{imakeidx}
\makeindex[columns=1]

\begin{document}

Winter has fled, grass \index{Winter has fled, grass}

Winter has fled; already \index{Winter has fled; already}

\printindex
\end{document}
2
  • Welcome to TeX.SX!
    – moewe
    Commented Nov 29, 2021 at 7:00
  • Comma , has preference over semi-colon ;, So, its absolutely logical. If you need your desired output, you can change or set a new define-group in your style file. You can read the documentation of the package.
    – mmr
    Commented Nov 29, 2021 at 8:09

2 Answers 2

2

You might define a command \indexnp (no punctuation) that automatically removes punctuation and forms the right construction with the @ specifier, so

\indexnp{Winter has fled, grass}

becomes

\index{Winter has fled grass@Winter has fled, grass}

Here's the code: the argument is stored in a token list variable, in which punctuation is stripped off. Then the wanted

\index{<text, no punctuation>@<text>}

is generated.

\documentclass[10pt]{octavo}
\usepackage{imakeidx}
\makeindex[columns=1]

\ExplSyntaxOn
\NewDocumentCommand{\indexnp}{m}
 {
  \egreg_indexnp:n { #1 }
 }

\tl_new:N \l__egreg_indexnp_tl

\cs_new_protected:Nn \egreg_indexnp:n
 {
  \tl_set:Nn \l__egreg_indexnp_tl { #1 }
  \regex_replace_all:nnN { [\.\,\;\:] } { } \l__egreg_indexnp_tl
  \__egreg_indexnp_do:Vn \l__egreg_indexnp_tl { #1 }
 }

\cs_new_protected:Nn \__egreg_indexnp_do:nn
 {
  \index{#1@#2}
 }
\cs_generate_variant:Nn \__egreg_indexnp_do:nn { V }

\ExplSyntaxOff

\begin{document}

Winter has fled, grass \indexnp{Winter has fled, grass}

Winter has fled; already \indexnp{Winter has fled; already}

\printindex
\end{document}

enter image description here

0
2

You can separate the string used for sorting from the typeset text with @

enter image description here

\documentclass[10pt]{octavo}
\usepackage{imakeidx}
\makeindex[columns=1]

\begin{document}

Winter has fled, grass \index{Winter has fled grass@Winter has fled, grass}

Winter has fled; already \index{Winter has fled already@Winter has fled; already}

\printindex
\end{document}
0

You must log in to answer this question.

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