# Error when using imakeidx and xwatermark

I was editing a text that carries a watermark and decided to add an index. Everything was going fine until at some point an error message came out. After debugging I could identify the issue.

There is some incompatibility between packages imakeidx and xwatermark when more than one page with indexes are presented, but not always. I mean, it seems to have a certain event, which I don't know, that returns an error when there is more than one index page AND a certain number of indexes. I must say this because sometimes there are two index pages and no error appears.

The erro mensage is something like:

Something's wrong--perhaps a missing \item. {\bf P

I checked the lines of the generated .ind file and nothing seemed to be wrong. All blocks follow the same structure.

  \indexspace
{\bf P}\nopagebreak%
\indexspace\nopagebreak%
\item p, \hyperpage{1}


If I change the number of columns to more than one, it won't have any error since all indexes fit in only one page. That's a nice workaround, but not a general solution. Another workaround is to prevent the watermark to be printed over index pages, by changing allpages for something like pages=1-3.

The following MWE reproduces the problem.

\documentclass{article}

\usepackage{imakeidx}
\makeindex[columns=1, options=-s style_file_imakeidx_alfa_01.ist]
% columns=2 works fine, since it doesn't need more than one page

\usepackage{tikz}
\usepackage[printwatermark]{xwatermark}
\newsavebox\myboxr\savebox\myboxr{\tikz[color=red!80,opacity=0.4]\node{\huge{Watermark}};}
\newwatermark[allpages, angle=45, scale=6, xpos=-10, ypos=10]{\usebox\myboxr}
% Another workaround: Don't print wateermark over index. Example: allpages <-> pages=1-3.

\begin{document}
Text.
\clearpage
a\index{a}
\clearpage
b\index{b}

c\index{c}

d\index{d}

e\index{e}

f\index{f}

g\index{g}

h\index{h}

i\index{i}

j\index{j}

k\index{k}

l\index{l}

m\index{m}

n\index{n}

o\index{o}

After letter p, if columns = 1, error. % Something's wrong--perhaps a missing \item. {\bf P

% (Un)comment these lines to change from error/no error when columns = 1
p\index{p}

q\index{q}

r\index{r}

\printindex
\end{document}


This is the file style_file_imakeidx_alfa_01.ist to generate the formatted index list (I don't if this setup contributes to the error).

quote '+'
delim_r "~--~"
suffix_2p "\\,f"
suffix_3p "\\,ff"
preamble
postamble "\n\n\\end{theindex}\n"


Of course I can continue working with or watermark or printindex, but not both at the same time, or even limiting the pages with watermark.

The question is: know what is going on and how to correct it? If possible.

• It seems that the issue arises during the output routine. I get no error and essentially identical output with draftwatermark instead. – egreg Sep 24 '20 at 19:47
• I also get no error. what is your TeX version? – user108724 Sep 24 '20 at 20:03
• I wouldn't use xwatermark, it loads catoptions which is incompatible with various other packages and LaTeX code, and will probably not work at all with the next LaTeX release. – Ulrike Fischer Sep 24 '20 at 20:41
• I normally use eso-pic to print stuff below or above text. With the next latex release I will perhaps use the new shipout/background and shipout/foreground hooks. – Ulrike Fischer Sep 24 '20 at 21:40

I followed @Ulrike Fischer's advice about eso-pic and now the watermark works with imakeidx and can be printed above the text (stamped).

Although it is still not clear the reason behind xwatermark's issue, I shall say this solution is very nice.

The following MWE show the answer.

\documentclass{article}

\usepackage{lipsum}
\usepackage{imakeidx}
\makeindex[columns=1, columnseprule, options=-s style_file_imakeidx_alfa_01.ist]
% columns=2 works fine, since it doesn't need more than one page

\usepackage{tikz}
\newsavebox\myboxr\savebox\myboxr{\tikz[color=red!80,opacity=0.4]\node{\huge{Watermark}};}

\usepackage{eso-pic}
\AtPageCenter{\put(-320,-230){\scalebox{7}{\rotatebox{45}{\usebox\myboxr}}}}%
\AtStockLowerLeft{\put(0,0){\scalebox{7}{\rotatebox{45}{\usebox\myboxr}}}}%
}

\begin{document}
Text.

\includegraphics{example-image-a}
\clearpage
a\index{a}
\clearpage
b\index{b} c\index{c} d\index{d} e\index{e} f\index{f}
g\index{g} h\index{h} i\index{i} j\index{j} k\index{k}
l\index{l} m\index{m} n\index{n} o\index{o} p\index{p}
q\index{q} r\index{r}

\lipsum[1-2]
\printindex
\end{document}


The following figures show the results.