# Include LaTeX file named with substring of current filename

I'm frequently creating presentations with beamer and need both the handout and the presentation itself. To avoid editing the file when I need to compile the other, I use \ifdefined\ishandout ... at the beginning of the presentation and have a separate file for the handout that contains nothing but

\def\ishandout{1}
\input{topic.tex}


Since the handout file is always named following the schema topic_handout.tex I'm looking for a way to strip the inner _handout so I can use the same handout file for all presentations without changing the content. I found \jobname which gives me topic_handout, so I just need to strip the constant string _handout and add .tex to the argument in \input. Since this happens before the document class is declared, I cannot use any packages. Any help is welcome.

-
Welcome to TeX.sx! Please use backticks  to mark inline code, file names etc. – Martin Scharrer Jun 28 '11 at 13:49
You actually can use packages before \documentclass by using \RequirePackage instead of \usepackage. But you should be careful anyway. Also you can use \PassOptionsToClass{handout}{beamer} and \mode<handout>{...} instead of \def\ishandout{1} and \ifdefined\ishandout ..., respectively. Note that \jobname returns the file name as characters (catcode 12, "other") and not as letters (catcode 11), which must be taken into account for string comparison and manipulation. – Martin Scharrer Jun 28 '11 at 13:49
Thanks for the hint, will use the backticks in the future. – Gerald Senarclens de Grancy Jun 28 '11 at 15:39

It's not very clear what the problem is; as I have understood it, you want that

pdflatex topic_handout


actually inputs topic.tex after defining \ishandout. This can be done all on the command line, without an auxiliary file topic_handout.tex:

pdflatex "\def\ishandout{1}\input{topic}"


If you don't want to use the command line, your topic_handout.tex file may be

\def\ishandout{1}

\edef\x{\def\noexpand\dehandout##1\detokenize{_handout}{##1}}\x
\expandafter\input\expandafter{\expandafter\dehandout\jobname}

-
+1, using \detokenize instead of \@onelevel@sanitize is a good idea. – Martin Scharrer Jun 28 '11 at 15:57

You can define a macro with _handout in the parameter text, so that this part is then removed. This requires a little more advanced coding. Here my try to explain it:

You need to use the plainTeX macro \def instead of LaTeX's \newcommand for this. One issue is that all characters except spaces in \jobname are returned with catcode 12 other and are not letters (catcode 11) as normal. To define a macro with the correct catcodes you can use e.g. LaTeX's \@onelevel@sanitize macro to turn the whole macro definition to catcode other. Then you need a lot of \expandafters to expand the helper macro before the removal macro is defined.

% somename_handout.tex
\def\handoutstr{_handout}
\makeatletter
\@onelevel@sanitize\handoutstr% to get right catcodes!
\makeatother
\expandafter\def\expandafter\removehandout\expandafter#\expandafter1\handoutstr\relax{#1}

%\def\ishandout{1}
% better:
\PassOptionsToClass{handout}{beamer}

\input{\expandafter\removehandout\jobname\relax}


In addition it is better to use \PassOptionsToClass{handout}{beamer} instead of \def\ishandout{1}. This passes the handout option directly to the beamer class. Then in the presentation file you can use \mode<handout>{ handout-only code }:

% somename.tex
\documentclass{beamer}

\mode<handout>{%
% handout-only code
}
\begin{document}
\begin{frame}
% ...
\end{frame}

% ...
%
\end{document}
`
-