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.

Is it possible to check if a file is listed inside \includeonly{...} ?

For example I would like to do something like this

\includeonly{file1,file2}
\if\isincluded file1       % \isincluded is a placeholder for the real test macro 
   \usepackage{apackage}
 \fi

 \begin{document}
 \include{file1}
 \include{file2}
 \end{document}

to avoid loading apackage when file1 is not included (because the package is expensive to load, like TikZ)

share|improve this question
    
Hi mljrg. Usually, we don't put a greeting or a "thank you" in our posts. While this might seem strange at first, it is not a sign of lack of politeness, but rather part of our trying to keep everything very concise. Upvoting is the preferred way here to say "thank you" to users who helped you. –  Claudio Fiandrino Feb 13 '13 at 19:59
    
Ok, I forgot that. Thanks –  mljrg Feb 13 '13 at 20:02
add comment

4 Answers

up vote 14 down vote accepted

The following macro \isincluded uses the same test that \include uses (code copied from there). If the file in the first argument will be included, then the second argument is called, otherwise the third. Also the macro catches the case, when \includeonly is not used and all files are included.

\documentclass{article}
\includeonly{file,file2}                     

\makeatletter
\newcommand*{\isincluded}[1]{%
  \@tempswatrue
  \if@partsw   
    \@tempswafalse
    \edef\reserved@b{#1}%
    \@for\reserved@a:=\@partlist\do
      {\ifx\reserved@a\reserved@b\@tempswatrue\fi}%
  \fi
  \if@tempswa
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\isincluded{file1}{%
  \usepackage{apackage}%
}{}

\begin{document}
\include{file1} 
\include{file2} 
\end{document}  

If \includeonly is used the switch \if@partsw is true. Then the files of \includeonly are available in \@partlist.

Extension: \isincluded with list of files

Use case: Resources (packages) must be loaded if some files are included. That means if either of the files in \isincluded will be included, then the test of \isincluded evaluates to true (comma is "or" operator):

\documentclass{article}
\includeonly{file1,file3}

\makeatletter
\newcommand*{\isincluded}[1]{%
  \@tempswatrue
  \if@partsw
    \@tempswafalse
    \edef\isincluded@list{\zap@space#1 \@empty}% removes spaces in file list
    \@for\reserved@b:=\isincluded@list\do{%
      \@for\reserved@a:=\@partlist\do{%
        \ifx\reserved@a\reserved@b\@tempswatrue\fi
      }%
    }%  
  \fi   
  \if@tempswa
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\isincluded{file2,file3}{%
  \usepackage{apackage}%
}{}

\begin{document}
\include{file1} 
\include{file2} 
\include{file3}
\end{document} 
share|improve this answer
    
GREAT! I have just added to my .cls file! Thanks –  mljrg Feb 13 '13 at 20:14
    
Is it possible to make \isincluded receive a list of files, e.g., \isincluded{file1,file3}? –  mljrg Feb 14 '13 at 10:42
    
@Heiko: The question says "is it possible to check if a file is listed inside \includeonly{...}". I am rather confused by the use of \@tempswatrue in your solution. If \includeonly isn't called, \@partsw is false but your \@tempswa will be true. –  Ahmed Musa Feb 14 '13 at 13:58
    
@AhmedMusa If \includeonly is not used, then this means all files are included in LaTeX. –  Heiko Oberdiek Feb 14 '13 at 19:52
    
@HeikoOberdiek I am not even close to being a TeX/LaTeX guru, so is it easy to change your definition of \isincluded to work with a list of files, such that \isincluded{file1,file3}{xxx}{yyy} will include xxx if either file1 or file3 are included, and yyy otherwise? Thanks –  mljrg Feb 15 '13 at 10:29
show 2 more comments

The \includeonly files are saved i an edef called \@partlist.

\documentclass{article}
\usepackage{filecontents}
\begin{filecontents}{test1.tex}
  file 1
\end{filecontents}
\begin{filecontents}{test2.tex}
  file 2
\end{filecontents}
\includeonly{test2, test1}
\begin{document}
\makeatletter
 \@partlist
\makeatother
\meaning\@partlist
\end{document}

Not too sure if this is of any help, but you can read the source2e routines.

share|improve this answer
    
I do not want to have all my content defined in only one file and then repeated in other files, as it seems from what I understand that filecontents does. –  mljrg Feb 13 '13 at 20:01
2  
@mljrg the filecontents is just for making a MWE. In this case it creates the dummy files test1.tex and test2.tex –  StrongBad Feb 13 '13 at 20:05
add comment

You could write your own wrapper around \includeonly that tests for the filename

\documentclass{article}
\usepackage{etoolbox}

\newcommand{\myincludeonly}[1]{%
  \def\do##1{\ifstrequal{##1}{file1}{\usepackage{tikz}}{}}
  \docsvlist{#1}
  \includeonly{#1}
}
\myincludeonly{file1,file2}

\begin{document}
\include{file1}
\include{file2}
\end{document}
share|improve this answer
add comment

Here is the issue I raised with Heiko's solution. In the following, ifpdf is input even though \includeonly is commented out.

\documentclass{article}
%\includeonly{file,file2} % commented

\makeatletter
\newcommand*{\isincluded}[1]{%
  \@tempswatrue
  \if@partsw
    \@tempswafalse
    \edef\isincluded@list{\zap@space#1 \@empty}%
    \@for\reserved@b:=\isincluded@list\do{%
      \@for\reserved@a:=\@partlist\do{%
        \ifx\reserved@a\reserved@b\@tempswatrue\fi
      }%
    }%
  \fi
  \if@tempswa
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\isincluded{file1}{%
  \usepackage{ifpdf}%
}{}

\begin{document}
\include{file1}
\include{file2}
\end{document} 

My approach is

\documentclass{article}
% Comment out or retain the following:
\includeonly{file,file2}                     

\makeatletter
\newif\if@tempswb
\newcommand*{\ifincluded}[1]{%
  \begingroup
  \@tempswbfalse
  \if@partsw
    \edef\@tempa{\zap@space#1 \@empty}%
    \@for\reserved@b:=\@tempa\do{%
      \@for\reserved@a:=\@partlist\do{%
        \ifx\reserved@a\reserved@b\@tempswbtrue\fi
      }%
    }%  
  \fi   
  \expandafter\endgroup
  \if@tempswb
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
}
\makeatother

\ifincluded{file1}{%
  \usepackage{ifpdf}%
}{}

\begin{document}
\include{file1} 
\include{file2} 
\end{document} 
share|improve this answer
    
The inclusion of ifpdf in this case is indeed a feature. Without \includeonly LaTeX includes all files file and file2, thus the test \ifincluded{file1} must evaluate to true. –  Heiko Oberdiek Feb 15 '13 at 11:03
    
@HeikoOberdiek: That is true, many thanks. But the letter of the question - "is it possible to check if a file is listed inside \includeonly{...}" - needed the clarification you have given latterly here. –  Ahmed Musa Feb 15 '13 at 17:37
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.