Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

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

1 Answer 1

up vote 2 down vote accepted

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.

enter image description here

share|improve this answer
    
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? –  Michał Apr 11 at 17:10
    
@Michal You have an outdated distribution. Upgrade it. For imakeidx I suggest you to look at its documentation. ;-) –  egreg Apr 11 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. –  Michał Apr 11 at 17:19
    
I finally was able to update my texlive. Your solution works perfectly so far. Thanks! –  Michał Apr 22 at 15:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.