# Is it possible to index an exclamation point?

I'm typesetting a manual on using the R programming language. As in a few other languages, ! is used as the logical operator for not. I'd like to have this symbol appear in the index, but it will not. It seems Latex interprets it as punctuation and ignores it.

I tried enclosing it in a \code{} block (a texttt command we've defined for the manual) but this results in an empty line being added to the index. (See below) I also tried \fun{$!$} but the document does not compile and I get an error message for the .ind file:
Line 4 "Missing $inserted" Line 5 "Missing$ inserted"

I've also looked for an alternative symbol to use, but it seems that Latex only includes \textexclamdown, and there is no \textexclamup.

\documentclass{report}

\RequirePackage{imakeidx}

%===----- Functions for indexing and typesetting -----=====
\newcommand{\fun}[1]{\texttt{#1}\index{#1}}
\newcommand{\FUN}[1]{\texttt{#1}\index{#1|textbf}}
\newcommand{\code}[1]{\texttt{#1}}

\makeindex

\begin{document}
\SweaveOpts{concordance=TRUE}

\chapter{Logical operators}

In this section, we would like to introduce you to some of the logical operators used in the R programming language. These include the \FUN{\&} operator, the \FUN{!} operator, and the \fun{\textbar{}} operator. (Note that \FUN{\code{!}} produces an empty space in the index.)

<<>>=
nums1 <- c(1,4,2,8,11,100,8)

# Subset of nums1, where value is exactly 8 or 11:
nums1[nums1 == 8 | nums1 == 11]

# Subset nums1 where number is NOT equal to 100
nums1[nums1 != 100]
@

Here is a table that summarizes the logical operators:

\begin{table}[!htbp]
\caption{Logical operators.} \label{table:logicops}
\begin{center}
\begin{tabular}{l l}
Operator                & Meaning \\ \hline
\fun{\textgreater{}}    & greater than\\
\fun{\textless{}}       & less than\\
\fun{\&}                & AND\\
\fun{==}                & equal to\\
\fun{\textbar{}}        & OR\\
\fun{\%in\%}            & is an element of\\
\fun{!}                 & NOT\\
\end{tabular}
\end{center}
\end{table}

\newpage
\printindex

\end{document}


• I believe you want to use \fun{"!} in this case. The " is used to 'quote' !, @, and |. – jon Aug 15 '16 at 2:08
• Thanks! This is a great solution. If you would like to post this as an answer I'd be happy to accept it. – Sasc Aug 15 '16 at 3:22

The characters !, @, and | need to be escaped in an indexing context. For makeindex to understand you mean to mention them rather than use them, you must 'quote' them with a ". The following should do the trick:

\index{"!} % or, in this case: \fun{"!}

• Thanks! Believe it or not, I couldn't find information about escaping characters except by using \! – Sasc Aug 15 '16 at 10:18
• Glad it helps! What you probably want to look at is the documentation for makeindex. On TeX Live, you can read this by typing texdoc makeindex in a terminal. – jon Aug 16 '16 at 2:51
• Thanks, that is a great and concise answer. – kcrisman Jun 28 at 21:51

I did some playing around, and the problem seems to stem from your \fun command, as I added in an $x$to see if it would print with this, and only prints the x. Also, adding spacing { ! } rather than {!} seems to break the formatting. I left it in so you can see it. Removing the \fun command also removes it from the index altogether, but I guess you probably worked that out yourself.

\documentclass{report}

\usepackage{imakeidx}

%\RequirePackage{imakeidx}

%===----- Functions for indexing and typesetting -----=====
\newcommand{\fun}[1]{\texttt{#1}\index{#1}}
\newcommand{\FUN}[1]{\texttt{#1}\index{#1|textbf}}
\newcommand{\code}[1]{\texttt{#1}}

\makeindex

\begin{document}
%\SweaveOpts{concordance=TRUE}

\chapter{Logical operators}

\begin{table}[!htbp]
\caption{Logical operators.} \label{table:logicops}
\begin{center}
\begin{tabular}{l l}
Operator                & Meaning \\ \hline
\fun{\textgreater{}}    & greater than\\
\fun{\textless{}}       & less than\\
\fun{\&}                & AND\\
\fun{==}                & equal to\\
\fun{\textbar{}}        & OR\\
\fun{\%in\%}            & is an element of\\
\fun{ ! x }                 & NOT\\
\end{tabular}
\end{center}
\end{table}


I hope this helps, I've never personally used an Index before.

I also had other problems with your code when compiling, so I commented everything out except the table. This could be my compiler, so I'll leave that bit to you :)

• Welcome to TeX.SX! Just a hint, use  for in line code. For blocks of code, use the code sample mark down, as I've done. Much neater and easier on the eye and applies syntax highlighting. Simply select the code and press Ctrl+k, or use the code sample button (fifth from the left, the one that looks like {}). Think of  as $...$ and code sample markdown as $...$ if it helps (that's how I like to think of it anyway :P ) – Au101 Aug 15 '16 at 2:00
• It's not necessarily "broken", however I would guess that it's a conflict of how the \index command works. It might be just the formatting does not allow for the ! character. Seems ridiculous, I know, but it's possible. – James Hewitt Aug 15 '16 at 2:35
• I'm not sure this can be attributed to our typesetting commands, as using the built-in command \index{!} also does not add ! to the index. Trying \index{$!$} led to the same 'Missing \$ inserted' error. – – Sasc Aug 15 '16 at 3:23
• This answer doesn't really address the issue. ! is (by default) a part of the index syntax so you need to escape it or use a makeindex style that specifies a different level separator character. – David Carlisle Aug 15 '16 at 8:50
• It does address the issue, but it doesn't fix it. But he found a solution, so that's what matters. – James Hewitt Aug 17 '16 at 0:24