# Custom verbatim commands writing into index

I have created a custom fancyvrb command via \CustomVerbatimCommand{\afun}{Verb}{} to use for writing programming language functions, say \afun|object.method.blah|, etc.

I would like to automatically build an index of all mentions of function names.

I know I could try to do that with ordinary LaTeX commands, e.g. with something like:

\newcommand{\afun}[1]{\texttt{#1}\index{#1@\texttt{#1}}}


That will create an index entry with a function name.

Is there any way to achieve that, but with custom verbatim commands of fancyvrb?

• Could you please be more specific with some typical examples? Do you really need verbatim mode? Apr 11, 2014 at 16:37
• @egreg, I'll put an example shortly. I think I need verbatim because of underscores in function names. Apr 11, 2014 at 16:47

I'd suggest using xparse:

\documentclass{article}
\usepackage{imakeidx}
\usepackage{xparse}

\makeindex

\NewDocumentCommand\afun{v}{%
\texttt{#1}\index{#1@\ifun{#1}}%
}
\NewDocumentCommand{\ifun}{v}{\texttt{#1}}

\begin{document}

\afun{object.method.blah}

\afun{object.method_2.blah}

\printindex

\end{document}


If you only need the underscore, among the special characters, this seems the cleanest approach.

• Looks good. I guess you meant package makeidx. However, when I tried to compile your example, I'm getting (xparse) Unknown argument type 'v' replaced by 'm'. Fingers crossed ... Any clues? Apr 11, 2014 at 17:10
• @Michal You have an outdated distribution. Upgrade it. For imakeidx I suggest you to look at its documentation. ;-) Apr 11, 2014 at 17:15
• indeed... even though it is texlive up to date with Ubuntu repositories. I will get back here as soon as I update. Thanks so far anyway! Your solution looks very promising. Apr 11, 2014 at 17:19
• I finally was able to update my texlive. Your solution works perfectly so far. Thanks! Apr 22, 2014 at 15:19