2

I've been reading something in Most useful sed one-liners for TeX users which is related to my problem, but albeit interesting, it is not what I need.

I have many tex (beamer class) files in subdirectories and I would like to comment some lines in each of them.

The purpose is to modify the preamble in order to compile the files to get the handouts as indicated in Passing parameters to a document answer 45

In each file I have the following lines, which I comment in and out as needed. The first one is to compile the beamer presentation while the others three are needed to compile for handouts

%here is some text, consistent in all the .tex files
%\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer} 
%
 \documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} 
 \usepackage{pgfpages}
 \pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

The four lines above should be commented out and the some lines should be appended. The desired .tex file should contains the following

%here is some text, consistent in all the .tex files
\ifdefined\ishandout
 \documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} 
 \usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
\else
  \documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer} 
\fi

%\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer} 
%
%\documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} 
%\usepackage{pgfpages}
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

Of course I could edit each file manually, but I would learn nothing.

I was successful in inserting the lines from \ifdefined to \fi, by pasting them into a file (saved as insert.txt) and then sending in a terminal (debian wheezy) the following line

sed  -i "/^%here is some text, consistent in all the .tex files$/ r ../insert.txt" lesson_10.tex

I tested it only on one .tex file, and it works

To add the lines to each file in each subdir, I wrote this bash script

   for i in ??_lesson*/ 
    do
        if [ -d $i ] 
            then                
                cd $i
            THIS=$(ls lesson_??.tex) 
                 sed  -i "/^%here is some text, consistent in all the .tex files$/ r ../insert.txt" $THIS
            fi
        done

Now I need a sed command that change/replace the lines

\documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} 
\usepackage{pgfpages}
\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

into/with

%\documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} 
%\usepackage{pgfpages}
%\pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

I've read somewhere on the internet that sed process a line at time, so I tried this one.

sed "s/%\documentclass/%\documentclass/g" lesson_01.tex > checkthis.tex; head checkthis.tex 

which returns

 \%documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer} 
 \%documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} 
 \usepackage{pgfpages}
 \pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

This other line was executed to check whether the problem was in the s or in the g part of the command

sed "s/%\documentclass/%\documentclass/g" lesson_01.tex > checkthis.tex; head checkthis.tex 

which returns

\%
  oo[xcolor={usenames, table, x11names}, final, 10pt]{beamer} 

 \%
   oo[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} 
 \usepackage{pgfpages}
 \pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]

It's evident that the "%" has a special meaning for sed, a meaning which I ignore.

I tried also some escaping characters before the %, but to no avail. Googling around was of no help, I therefore decided to ask for a direct help here.

5
  • 3
    This seems to be a sed question rather than tex so I suspect it's off topic. % is not special to sed but \ is. Commented Apr 7, 2014 at 16:28
  • 2
    % is a special character for most shells, though, so if you pass it on the command line (rather than in a script), it may have unexpected effects. Try sed -e 's/^\(\\documentclass\)/\%\1/' -e '/^\%\\documentclass/,/\\pgfuselayout/s/^\(.*pgf\)/\%\1/' (not fully tested so don't use -i until you are sure).
    – cfr
    Commented Apr 7, 2014 at 16:36
  • For David. I was afraid of an OT myself too, but after googling extensively and not finding an answer, I decided to write here, where the problems with "%" are more probable to occur. For cfr: the code works and the outcome is a s follows { %\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer} \documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} % \usepackage{pgfpages} % \pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm]
    – Ottorino
    Commented Apr 8, 2014 at 6:59
  • Sorry for the double post. I'm learning the local syntax for comments. Here a more readable version (I hope ...) @cfr: the code works partially. Here's the outcome: %\documentclass[xcolor={usenames, table, x11names}, final, 10pt]{beamer} \documentclass[xcolor={usenames, table, x11names}, handout, 10pt]{beamer} % \usepackage{pgfpages} % \pgfpagesuselayout{2 on 1}[a4paper,border shrink=5mm] Only one occurrence of \documentclass has changed. Could you be please so kind to bear with me a little more and explain briefly the syntax ?
    – Ottorino
    Commented Apr 8, 2014 at 7:14
  • @fcfr. The code works perfectly. It's my fault: I tested it on the wrong file. The first occurrence is ^\documentclass... while the second one is not at the beginning of the line, but it have some spaces before !! ^ \documentclass...
    – Ottorino
    Commented Apr 8, 2014 at 7:31

0

Browse other questions tagged .