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.

So here is the situation : I wanted latex to read some commands when I include another file, and for that I defined a new \include command like this :

\newcommand{\foo}[1]{}

\newcommand{\includeas}[2][]{%
   \foo{#1}
   \include{#2}}

It works beyond all hopes! But now I'd like to still be able to use RefTeX's toc browser with my new \includeas command.

And here is the problem : (from RefTeX's manual)

User Option: reftex-include-file-commands

List of LaTeX commands which input another file. The file name is expected after the command, either in braces or separated by whitespace.

As you can see, having an optional argument in square brackets after the macro is a problem for RefTeX.

Is there any way I can bypass it?

At the moment I have a quick fix by turning the optional #1 argument to mandatory #2, but it relies on the fact I actually provide the argument all the time, which won't always be the case.

MWE:

File main.tex

\documentclass{minimal}

\begin{document} 

\includeas[blabla]{foo} 
 % The test is to press C-C = , not to try to compile!

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:

File foo.tex :

\chapter{Name}
\label{cha:name}
share|improve this question
add comment

1 Answer

up vote 4 down vote accepted

You can make the argument optional on the flip side of the command:

\makeatletter
\def\includeas#1{
    \@ifnextchar[{\includeas@opt{#1}}{\includeas@{#1}}
}
\def\includeas@#1{ 
    \include{#1}
}
\def\includeas@opt#1[#2]{
    \foo{#2}
    \include{#1}
}
\makeatother

Then you will call it as:

Without optional
\includeas{filename.tex}
With optional
\includeas{filename.tex}[foo]

However you should not start a regular sentence with [ after a use of the command \includeas.

share|improve this answer
    
Awesome, thank you very much. You last warning is right, but it won't happen, there is about nothing in my main file. One thing only, for the command to have the intended input, I had to replace the second definition by \def\includeas@#1{ \foo{Default} \include{#1} } No big deal though. Thanks again! –  T. Verron Dec 1 '11 at 14:43
    
Ahh. If you need a default value i would do like this instead: \makeatletter \def\includeas#1{ \@ifnextchar[{\includeas@opt{#1}{\includeas@opt{#1}[Default]} } \def\includeas@opt#1[#2]{ \foo{#2} \include{#1} } \makeatother –  zeroth Dec 1 '11 at 14:49
    
Ok thanks, I can see the advantages of this version. :) –  T. Verron Dec 1 '11 at 16:47
add comment

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.