# Add line to index file to avoid page number

Is it possible to write custom lines into the index-file?

Consider this:

\documentclass{scrbook}
\usepackage[splitindex]{imakeidx}

\makeindex[name=date]

\newcommand\dateindex[1]{\index[date]{#1}}

\begin{document}
\section{Diary, April 4th, 1891}
\dateindex{1891!04!27}

Just some text

\immediate\write\@indexfile{???}{}

\printindex[date]
\end{document}


My question is: How do i change this

\immediate\write\@indexfile{???}{}


to write something without pagenumber into the idx file?

Of course this does not make any sense as I could just use an sty-file. But in my final version I would have dates and page numbers for those part of the book that are not within a diary entry.

• What kind of output do you want? Do you want to have the index entry just list the date? – Teepeemm Oct 21 '16 at 14:05
• Exactly! That's what I am looking for – Martin Mueller Oct 21 '16 at 14:07

this example shows a simple method for suppressing the page number, as well as the comma.

\documentclass{article}
\usepackage{makeidx}
\newcommand{\idxnopage}[1]{{}}
\providecommand{\gobble}[1]{{}}
\makeindex

\begin{document}
Here is some text\index{some text} that we want indexed.

We also want a header'' in the index.

\printindex
\end{document}


the resulting index:

of course, you are responsible for creating an appropriate sort field so these entries sort into the desired order.

update: according to a comment by @florian, this does not work with xindy.

it does work with imakeindex as long as makeindex is used as the back end.

• For whoever else might be trying to use this solution: Unfortunately it doesn't work with xindy which ignores "unknown attribute idxnopage'!" – Florian Aug 21 '18 at 12:24
• I.e.: not directly: To use it you'll need to define idxnopage in the style file used, cf below – Florian Aug 21 '18 at 12:31
• @Fiorian -- i'll add a note to the answer to say it works only with makeindex, and not with xindy. you say that to use xindy one "needs to define \idxnopage in the style file uses", but you're not specific which style file. please be specific (and that would be a userul answer). – barbara beeton Aug 21 '18 at 12:38
• You saved my day. I would like to add one thing: it is very tempting to define a \newcommand{\iindex}[2]{\index{#1@\idxsechead{#2} |idxnopage}}, but it won't work. One have to leave it as it is. – Mu-Tsun Tsai Jun 16 '20 at 6:30

This provides \dateindex for adding the page number and \dateindex* for suppressing it.

\documentclass{scrbook}
\usepackage{imakeidx}

\makeindex[name=date]

\makeatletter
\newcommand\dateindex{\@ifstar{\dateindex@noshow}{\index[date]}}
\newcommand\dateindex@noshow[1]{\index[date]{#1|noshowdate}}
\let\noshowdate\@gobble
\makeatother

\begin{document}

Not in the diary\dateindex{1892!04!27}\dateindex{1892!05!27}

\section{Diary, April 4th, 1891}
\dateindex*{1891!04!04}

\section{Diary, April 5th, 1891}
\dateindex*{1891!04!05}

Just some text

\printindex[date]

\end{document}


If you want to suppress the comma in the “no page” entries, you need a style file.

\begin{filecontents*}{checkcomma.ist}
delim_0 "\\checkcomma"
delim_1 "\\checkcomma"
delim_2 "\\checkcomma"
\end{filecontents*}

\documentclass{scrbook}
\usepackage{imakeidx}

\makeindex[name=date,options=-s checkcomma]

\makeatletter
\newcommand\dateindex{\@ifstar{\dateindex@noshow}{\index[date]}}
\newcommand\dateindex@noshow[1]{\index[date]{#1|noshowdate}}
\let\noshowdate\@gobble
\newcommand\checkcomma{\@ifnextchar\noshowdate{}{, }}
\makeatother

\begin{document}

Not in the diary\dateindex{1892!04!27}\dateindex{1892!05!27}

\section{Diary, April 4th, 1891}
\dateindex*{1891!04!04}

\section{Diary, April 5th, 1891}
\dateindex*{1891!04!05}

Just some text

\printindex[date]

\end{document}
`
• Great, thanks! Is there a possibility to remove the comma at the end? Or do I have to use a style file? – Martin Mueller Oct 24 '16 at 8:18
• @MartinMueller For that you need a makeindex style. I think it has already been covered on the site. – egreg Oct 24 '16 at 8:34