[Tex/LaTex] Comment out lines without using % and comment enviroment

commentsexercisesjobname

I would like to ask something which will greatly facilitate my work. But before, I introduce you to the problem I face. In my attempt to gather my notes for my students and give them something more quality like a book I've created hundreds of exercises where each one is a small own file. I input those through \input{...}.

The problem I face is that I have in the first 2 or 3 lines of each separate file certain information such as, source of exercise, as the name of the file in which the exercise is, etc. That information I do not want to appear in the final result, so I comment those out with %. Occasionally, however, as I still processing the text I want to appear these private information I have on the first lines on each separate file. As you can imagine being open about 500+ files and make comment out % the fist and second lines and after the inverse is somewhat painful.


Here's my question: Is there inner way to Latex to read a file that is imported but ignore the first two maybe three lines that have not been comment out with % ? Of course this could be done with a scrip from the command line by adding % in front of firsts lines ( Ι working on Linux) but what was very elegant and easy to do with some internal switch to LaΤeΧ . So I can choose when the first lines will appear and when not.


one example of how I structured each exercise

\\ {\color{red}{\small is the  exercise.435.tex}}  % is the name of the file
\\ {\color{red}{\small 1.84 ex. 127 page notes}}  % is the source of exercise etc

\begin{exercise}    
. . . 
\end{exercise}

Final With your help and after two days.

I have collect here the final form I used eventually. I've added the automatization of detection exercises's names . Perhaps new users of latex as me , find something useful

( I wrote here because I didn't know the right place to write those thinking. if is there another option please let me know )

\documentclass[a4paper,10pt]{article}
\usepackage{xcolor}

\usepackage[realmainfile]{currfile}[2012/05/06] % detects the names of imported files

%%%%%%%%%%%%%%%%%%%%%%%%%% for prologue ( The code is from the @egreg ) %%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newif\ifprolog  
\long\def\startprolog#1\stopprolog{%
  \ifprolog
    \par
    \begingroup
    \let\\\par
    \color{red}\small \\  \currfilepath \\ #1    % \currfilepath for appear the name of each exersice is my extra  addition 
    \par\medskip                          
    \endgroup                                 
  \fi}

\prologtrue % for to display the prologue ( comment out to disappear )

%%%%%%%%%%%%%%%%%%%%%%%%%% definition of exercise %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\newtheorem{exercise}{\color{magenta} \Large \bfseries} % [chapter]    %[section]   %% at the book documentclass 


\begin{document}

 text above the exercise

 \input{exersice.435.tex}

 Text below the exercise

 \end{document}

and the external file exersice.435.tex have the form:

\startprolog
  1.84 ex. 127 page notes   % is the source of exercise etc
\stopprolog

\begin{exercise}
 Here is the text of the exercise
\end{exercise}

the final result

Best Answer

If your input files all have the same form, that is some lines starting with \\ and an empty line after them, you can do by temporarily redefining \\ and changing the command for inputting the exercises:

\newcommand{\exeinput}[1]{%
  \let\latexdoublebackslash\\
  \ifprolog\let\\\showprolog\else\let\\\hideprolog\fi
  \input{#1}}

\def\showprolog#1\par{%
  \def\\{\par\noindent}% redefine `\\` to end lines
  \par\noindent
  #1% print the lines
  \par % end the last line
  \let\\\latexdoublebackslash}

\def\hideprolog#1\par{\let\\\latexdoublebackslash} % throw away the prolog lines

\newif\ifprolog

So you can say

\exeinput{exercise.435.tex}

and if you set \prologtrue also the prolog lines will be printed.

However this would spectacularly fail if there's not an empty line between the prolog and the main contents, so a special markup is probably the best strategy. For instance, if your exercise files have the form

\startprolog
\\ is the  exercise.435.tex % is the name of the file
\\ 1.84 ex. 127 page notes  % is the source of exercise etc
\stopprolog

\begin{exercise}    
. . . 
\end{exercise}

you can say

\newif\ifprolog
\long\def\startprolog#1\stopprolog{%
  \ifprolog
    \par
    \begingroup
    \let\\\par
    \color{red}\small #1
    \par\medskip
    \endgroup
  \fi}

and so

\input{exercise.435.tex}

will print the exercise without the prolog unless you set \prologtrue. The advantages are that you risk nothing if the prolog is not separated from the contents and that it's possible to reformat freely the prolog when you want to print it (maybe as a marginal note, for instance).

Related Question