1

I get really annoyed when I have to go find my tex template or download the template or recreate one from memory. Do Latex cli tools exist for linux where, in the terminal, I can write something like:

latex new article --name MyArticle

and it will initialize the necessary tex files.

  • 4
    many editors will have that feature certainly in emacs you can define a default initial new document template – David Carlisle Dec 14 '18 at 1:27
4

IMHO, the best way is to design your own script because you often need different packages and options. For my convenience, I designed one named edit.sh which calls for the editor if the file exists and create a new .tex file on-the-fly, according to my needs, if it doesn't. The advantage of shell script is adapting your file to your peculiar needs

Here is my edit.sh

#!/bin/ksh
#if the file doesn't exist create it with a template script
if ! [[ -f $1 ]]; then     
    #checks file extension
    case $1 in
   # for .tex calls for cnv_tex.sh  
   *.tex ) cnv_tex.sh $1;;#echo $1 | cnv_tex.pl ;;
   #shell
   *.sh ) echo "#!/bin/ksh" > $1;;
    esac ;
fi
#edit the file with my default editor
$VISUAL $1;;

I used to switch from time to time from Emacs to Vim, now I am not editor dependent and won't have to configure editor-specific templates.

And here is an extract of my very long cnv_tex.sh. I bypass the font selection mecanism, but let the language selection mecanism. To sum up : it asks for engine (ConTeXt, LaTeX, XeTeX or LuaTeX). If ConTeXt is chosen, it asks for my purposes and load the proper environments. If XeTeX, LaTeX or LuaTeX are chosen, asks for documentclass and languages, and fill the templates according to those. And yes, I am no shell wizard, so my script could be more elegant.

#!/bin/ksh

fichier=$1
#function to escape backslash while writing
function bremp {
   echo -E $1 >> ${fichier} ;}
#another one to unescape backslash
function rempl {
   echo $1 >> ${fichier} ;}
#engine
echo "[l]atex, l[u]alatex, [x]elatex ou [c]ontext ?"
read moteur

#create an array with used languages
echo "langue1 puis autres? en fr de pl ou nom entier"
read babel
set -A langues $babel 

#ConTeXt 
if [[ $moteur == 'c' ]] ; then 
   select finalite in article metapost atelier_informatique évaluation documents fiche_methodo sprawdziany cours_lfv buffer  ;
   do
      case $finalite in
         évaluation)
            cp $HOME/texmf/tex/context/cnv_eval.tex ${fichier} ;;
         (article|metapost) 
            if [[ ${finalite} == "article" ]] ;
            then bremp "\setuppapersize[A4]"
                 bremp "\setuplayout[width=17cm,height=27cm,"
                 bremp "   backspace=2cm,topspace=2cm,header=0cm]"
                 bremp "\setuphead[title][page=no,align=middle,textstyle=bold]"
                 bremp "\setuphead[section][textstyle=bold]" ;
            fi ;
            if [[ ${langues[0]} == "fr" ]] ; 
            then rempl "\setuplanguage[${langues[0]}]\n   [leftquote=\upperrightdoublesixquote"
                 bremp "    rightquote=\upperrightdoubleninequote]";
            fi
            rempl "\language[${langues[0]}]"
            rempl "\n\n\n\\starttext" 
            if [[ ${finalite} == "metapost" ]] ;
            then bremp "\startMPpage"
                 rempl "\n%variables\n"
                 bremp "\stopMPpage" ;
            fi
            bremp "\stoptext" ;;
         buffer) 
            echo "nom du tampon (buffer) ?" 
            read NomTampon
            rempl "\startbuffer[${NomTampon}]\n\stopbuffer" ;;
        #loading others environment
            *) bremp "\environment env_${finalite}"
               rempl "\n\n\starttext\n"
               bremp "\stoptext" ;;
         esac
      break ;
   done ; 
#LaTeX and Co
else
#classes
   select classe in article beamer lfv report book draft moderncv ltugprocp ;
   do case $classe in
      (article|book|draft)
         mainopt=12pt
         set -A packages [margin=2cm]{geometry} {url} {graphicx} ;;
      beamer)
         mainopt=11pt,noamsmath
         set -A masques usetheme{} usecolortheme{} useinnertheme{} useoutertheme{} \
            setbeamertemplate{blocks}[rounded] setbeamertemplate{navigation%symbols}{} ;;
      lfv)
         mainopt=12pt,noamsmath,t
         set -A packages {lfv}
         select couleurs in magpie frigatebird cormorant;
         do break ;
         done ;;
      report)
         mainopt=12pt
         set -A packages [a4paper,vmargin={3cm,3.5cm},inner=3cm,outer=2cm]{geometry} {url} {graphicx};;
      ltugprocp)
         mainopt=polish
         set -A packages {url};;
      esac
      break;
   done 

   #title 
   echo "Titre?"
   read titre
   echo "Auteur? ([d]t)"
   read auteur
   case $auteur in
       "d") auteur=Damien\ Thiriet;;
   esac

   #let's write it on the file
   bremp "\documentclass[${mainopt}]{${classe}}" 
   for i in ${packages[*]}; do
      bremp "\usepackage$i"  ; 
   done
   for i in ${masques[*]}; do
      rempl "%\\${i}" ;
   done
   case $classe in
      (beamer)
         #navigation symbols
         /usr/bin/sed -i '/navigation/s/%symbols/ symbols/' ${fichier} ;;
      (lfv)
         /usr/bin/sed -i '/documentclass/s/lfv/beamer/' ${fichier}
         bremp "\usecolortheme{${couleurs}}";;
   esac


   ############languages
   #loop to load languages
   i=0
   while [[ ${langues[$i]} != '' ]] ; do
   #convert codes to babel and polyglossia language names
    case ${langues[$i]} in
        "de") langues[$i]=german;;
        "en") langues[$i]=english;;
        "fr") langues[$i]=french;;
        "pl") langues[$i]=polish ;;
    esac
    #increment
    ((i++));
   done 
  #how many languages
   maxi=${#langues[*]}
   case $moteur in
      (x|u)
         #load luaotfload if luatex
         rempl "\n\usepackage{ifluatex}" 
         bremp "\ifluatex"
         bremp "   \usepackage{luaotfload}"
         bremp "\fi"
         #polyglossia
         bremp "\usepackage{polyglossia}" 
         #language options
         if (( $maxi == 1));
         then 
            rempl "\setdefaultlanguage{${langues[0]}}\n\n";
         elif (( $maxi == 2 ));
         then
            bremp "\setmainlanguage{${langues[0]}}"
            rempl "\setotherlanguage{${langues[1]}}\n\n";
         elif (( $maxi >= 3 ));
         then
            bremp "\setmainlanguage{${langues[0]}}"
            #je supprime la première langue du vecteur
            langues[0]=''
            #comme cela langue[*] donne les langues secondaires, tr remplace espace par ,
            rempl "\setotherlanguages{$(echo ${langues[*]}|tr ' ' ',')}\n\n";
         fi

      "l")
          for i in $langues;
          do
             case $i in
              polish)
                  bremp "\usepackage{polski}";;
              francais)
                  bremp "\usepackage{lmodern}";;
              esac
          done
          bremp "\usepackage[utf8]{inputenc}"
         if (( $maxi == 1));
         then 
              bremp "\usepackage[${langues[0]}]{babel}";
          else
             #je place la langue principale en paramètre à part
             principale=$langues[0]
             #ce qui me permet de la supprimer pour obtenir un vecteur avec les langues secondaires
             langues[0]=''
             bremp "\usepackage[$(echo ${langues[*]}|tr ' ' ','),$principale]{babel}";
           fi;;
   esac

   #call for bibliography (after polyglossia)
   case $classe in
      (beamer|book|report|article|draft|ltugprocp)
         bremp "%\usepackage[sorting=none]{biblatex}"
         bremp "%\bibliography{}";;
   esac

   #last infos
   rempl "\\\title{$titre}\n\\\author{$auteur}\n\date{}\n\n"
   rempl "\\\begin{document}\n\\maketitle\n\n\end{document}";
fi

I make also a heavy use of shell scripts from my editor. If you read polish, I wrote a paper for the GUST (Polish User Group) on this topic.

  • Nice script. Just a comment concerning language names: For babel it should be ngerman instead of german – TeXnician Dec 14 '18 at 14:30
  • @TeXnician : thanks. Didn't notice, because I use polyglossia in first place… and write very rarely in german. – sztruks Dec 14 '18 at 18:49

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.