4

I have several files containing each an exercise and to make a document I include a set of this files. Some of them need a special treatment so I need a to make a conditional input command that test a keyword on the first line of this file before include it.

Some files begin with :

\begin{minipage}

The other :

something else

I want to add an \item before the inclusion only if the file doesn't begin with \begin{minipage}

  • Related: ctan.org/pkg/catchfile – Henri Menke Apr 14 '16 at 20:48
  • are you committed to that syntax? It would be a bit easier if first lines were \typeA and \typeB, not comments. – David Carlisle Apr 14 '16 at 20:48
  • @DavidCarlisle In fact I need to know if the first latex command is \begin{minipage or not. – Tarass Apr 14 '16 at 20:55
  • @HenriMenke I don't understand what is the purpose of this package. – Tarass Apr 14 '16 at 20:56
  • @Tarass With that package you can read a file to a macro which you can then parse for specified tokens. – Henri Menke Apr 14 '16 at 21:07
3

A proof of concept, as details are missing from the question. The \tarassinput command inputs the file only if the first line agrees with the second argument.

\begin{filecontents*}{typea.tex}
% type A
here latex stuff
\end{filecontents*}

\begin{filecontents*}{typeb.tex}
% type B
here latex stuff
\end{filecontents*}

\documentclass{article}

\makeatletter
\newread\tarass@read
\newcommand{\tarassinput}[2]{%
  % #1 is the name of the file, #2 is the first line
  \openin\tarass@read=#1\relax
  \begingroup\catcode`\%=12
  \edef\tarass@tempa{\@percentchar\space#2\space}%
  \read\tarass@read to\tarass@tempb
  \closein\tarass@read
  \ifx\tarass@tempa\tarass@tempb
    \expandafter\@firstoftwo
  \else
    \expandafter\@secondoftwo
  \fi
  {\endgroup\input{#1}}%
  {\endgroup}%
}
\makeatother

\begin{document}

Yes: \tarassinput{typea}{type A}

No: \tarassinput{typeb}{type A}

\end{document}

enter image description here

Now that more information is available, here's a possibility with l3regex:

\begin{filecontents*}{typea.tex}
here latex stuff
\end{filecontents*}

\begin{filecontents*}{typeb.tex}
\begin{minipage}{5cm}
here latex stuff
\end{minipage}
\end{filecontents*}

\documentclass{article}
\usepackage{xparse,l3regex}

\ExplSyntaxOn
\NewDocumentCommand{\tarassinput}{m}
 {
  \tl_set_from_file:Nnn \l_tarass_input_tl { } { #1 }
  % \A matches the start
  % \c{begin} matches \begin
  % \cB. matches any "group begin" token
  % \cE. matches any "group end" token
  \regex_match:nVF { \A \c{begin} \cB. minipage \cE. } \l_tarass_input_tl { \item }
  \tl_use:N \l_tarass_input_tl
 }
\cs_generate_variant:Nn \regex_match:nnF { nV }
\ExplSyntaxOff

\begin{document}

\begin{itemize}

\item Start

\tarassinput{typea}

\tarassinput{typeb}

\end{itemize}

\end{document}

enter image description here

  • My mistake, I edit my question to be more specific. – Tarass Apr 14 '16 at 21:03
  • Why the \space after #2 in \tarass@tempa? Does TeX automatically convert the newline to space even though it is at that point undecided whether a second newline will follow? – Henri Menke Apr 14 '16 at 21:04
  • @HenriMenke Because of the end-of-line. I do know about it, don't I? ;-) – egreg Apr 14 '16 at 21:06
  • @egreg Works just fine. I add an optional parameter for the item[...]. Thanks for the comments, I will take a look at l3 syntax and son on. – Tarass Apr 14 '16 at 22:19
  • @egreg Need some more help on it with vertical spacing between minipages, see tex.stackexchange.com/questions/304398/… – Tarass Apr 15 '16 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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