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}

Tarass
  • 16,912

1 Answers1

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

egreg
  • 1,121,712
  • 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 http://tex.stackexchange.com/questions/304398/vertical-spacing-in-list-latex3-syntax – Tarass Apr 15 '16 at 08:53
  • @Tarass Sorry, but I don't understand what you're trying to do. What “usual space” are you referring to? Maybe http://tex.stackexchange.com/questions/34971/how-to-keep-a-constant-baselineskip-when-using-minipages-or-parboxes can be useful – egreg Apr 15 '16 at 09:03
  • @egreg Yes it is exactly the problem. You can see that the third point is too close to the second. I want to prevent that. I'll study your link. – Tarass Apr 15 '16 at 09:11
  • @egreg I understand what is in the link, but I'm stuck with l3 syntax. I read the begining of the doc, and understand the name syntax with the _ and :, but don't clearly sen the groups and the real meaning of each command you used. – Tarass Apr 15 '16 at 09:33
  • @egreg My idea is to save \prevdepth in the command and restore in on the next item use. There is nothing else between to tarassinput. – Tarass Apr 15 '16 at 09:36
  • @egreg You said in the link For the bottom you can reinstate after the minipage the depth of the last row. But the problem comes before the minipage. Then \prevdepth is not the solution, is it ? – Tarass Apr 15 '16 at 09:49
  • @Tarass I have no idea whatsoever about your aim, apart from this strange request of conditionally adding \item. Anyway, your \begin{minipage} should be \begin{minipage}[b]; before \end{minipage} you need to add \par\edef\tpd{\the\prevdepth} and after \tl_use:N \l_tarass_input_tl you need \par\prevdepth\tpd in case of a minipage. But this problem appears to me like something I'd never try to do. – egreg Apr 15 '16 at 09:56