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 :


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 Commented Apr 14, 2016 at 20:48
  • are you committed to that syntax? It would be a bit easier if first lines were \typeA and \typeB, not comments. Commented Apr 14, 2016 at 20:48
  • @DavidCarlisle In fact I need to know if the first latex command is \begin{minipage or not.
    – Tarass
    Commented Apr 14, 2016 at 20:55
  • @HenriMenke I don't understand what is the purpose of this package.
    – Tarass
    Commented Apr 14, 2016 at 20:56
  • @Tarass With that package you can read a file to a macro which you can then parse for specified tokens. Commented Apr 14, 2016 at 21:07

1 Answer 1


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.

% type A
here latex stuff

% type B
here latex stuff


  % #1 is the name of the file, #2 is the first line
  \read\tarass@read to\tarass@tempb


Yes: \tarassinput{typea}{type A}

No: \tarassinput{typeb}{type A}


enter image description here

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

here latex stuff

here latex stuff


  \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 }



\item Start





enter image description here

  • My mistake, I edit my question to be more specific.
    – Tarass
    Commented Apr 14, 2016 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? Commented Apr 14, 2016 at 21:04
  • @HenriMenke Because of the end-of-line. I do know about it, don't I? ;-)
    – egreg
    Commented Apr 14, 2016 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
    Commented Apr 14, 2016 at 22:19
  • @egreg Need some more help on it with vertical spacing between minipages, see tex.stackexchange.com/questions/304398/…
    – Tarass
    Commented Apr 15, 2016 at 8:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .