SEARCH

Enter your search query in the box above ^, or use the forum search tool.

You are not logged in.

#1 2010-10-22 21:55:35

jmbarnes
#! Junkie
Registered: 2009-05-04
Posts: 250

For chronic LaTeX compilers: a script

As I write my thesis in LaTeX (feel like the only social sciences person left doing such) I like to compile frequently--hence this script.

The script compiles a .tex file to .pdf using dvips and ps2pdf.  It has support for bibliographies, and optional creation of .rtf and .html files to go with the .pdf.

Outline of the script:

  • Prompt for directory

  • Create optional, dated .tar backup in backups folder

  • Select .tex file (automatic if there is only 1 .tex in the directory)

  • If .bib exists, asks if you want to use it. Select .bib (or automatic if only 1 .bib in directory)

  • Run the actual compilation(s)

  • Create optional .rtf file (using latex2rtf package)

  • Create optional .html folder/file (using latex2html package)

  • Prompt to open newly created .pdf is so desired

You can grab the main script ere: http://crunchbanglinux.org/pastebin/808
including helpful revisions by luc.

Welcome feedback, ideas, extensions, etc.

Last edited by jmbarnes (2010-10-23 17:52:21)


IRC: PizzaAndWine     Script bits: Incremental Backup | Sleep Timer

Offline

Help fund CrunchBang, donate to the project!

#2 2010-10-23 14:07:59

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: For chronic LaTeX compilers: a script

Hello jmbarnes,
nice idea for a script. I like it! But being the perfectionist that i am, i could not resist to make some modifications (and more modifications).
explanation:

  • Added quotes to handle spaces in filenames

  • changed questions to include default answers

  • make use of the "[[  ]]" bash construct and the "=~" regex comparison

  • removed unnecessary variables (TEXNUMBERS, BIBNUMBERS) and unnecessary pipes and greps

one big problem remains: the "select" procedure does not jet handle filenames correctly if they contain spaces.
and an idea: you could add some command line options to allow for a non interactive or half interactive use of the script. eg:

./latexscript -d=DIRECTORY -f=FILE --bib --html --rtf --open

cheers
luc

Last edited by luc (2010-10-23 14:40:30)

Offline

#3 2010-10-23 14:51:58

jmbarnes
#! Junkie
Registered: 2009-05-04
Posts: 250

Re: For chronic LaTeX compilers: a script

Many thanks -- a number of useful streamlining edits.

I like the idea of the command line options -- just need to read up on how to do such. Check back later today -- if it isn't too hard.


IRC: PizzaAndWine     Script bits: Incremental Backup | Sleep Timer

Offline

#4 2010-10-23 15:29:21

Erik
#! CrunchBanger
From: Fort Worth, TX
Registered: 2009-03-28
Posts: 176

Re: For chronic LaTeX compilers: a script

Not to piss you guys off, but may I ask why you decided to make this script? I use TexMaker and this has this kind of stuff build in, as do most latex editors.

Thumbs up though for writing your thesis in latex!

Offline

#5 2010-10-23 15:33:38

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: For chronic LaTeX compilers: a script

@erik
I don't know about jmbarnes, but for me making a script to automate a workflow is much more fun, than using a ide (i use my computer more for playing="learning how it works" than working)

edit just thought of a generic reason: if you have a small or light system a big ide might not be an option so if you still want to have your stuff automated ...

Last edited by luc (2010-10-23 15:40:34)

Offline

#6 2010-10-23 15:50:57

jmbarnes
#! Junkie
Registered: 2009-05-04
Posts: 250

Re: For chronic LaTeX compilers: a script

A couple of quick comments:
1) luc, rolled back to my script version for a bit... a few errors yours is throwing up.
2) I think implementing command line arguments would be too hard with the getopts function. Anyone know a way to check if a variable is empty that is quicker than writing if/then statements for each variable and only asking for info if that variable isn't full?
3) On writing this script -- yes there are lets of cool Tex programs out there (TeXila being my favorite.) That said there are multiple reasons for this. First, its highly mobile. Second, it automatically reminds me to make backups. Third, I write in console based Vim (and vim-latexsuite isn't my cup of tea.) Perhaps the most important of all though is I'm simply trying to find little projects that increase my bash skills. Solving little problems like this (or the sleep timer I needed) is all the training I have in scripting/programing.


IRC: PizzaAndWine     Script bits: Incremental Backup | Sleep Timer

Offline

#7 2010-10-23 16:20:36

Erik
#! CrunchBanger
From: Fort Worth, TX
Registered: 2009-03-28
Posts: 176

Re: For chronic LaTeX compilers: a script

jmbarnes wrote:

A couple of quick comments:
1) luc, rolled back to my script version for a bit... a few errors yours is throwing up.
2) I think implementing command line arguments would be too hard with the getopts function. Anyone know a way to check if a variable is empty that is quicker than writing if/then statements for each variable and only asking for info if that variable isn't full?
3) On writing this script -- yes there are lets of cool Tex programs out there (TeXila being my favorite.) That said there are multiple reasons for this. First, its highly mobile. Second, it automatically reminds me to make backups. Third, I write in console based Vim (and vim-latexsuite isn't my cup of tea.) Perhaps the most important of all though is I'm simply trying to find little projects that increase my bash skills. Solving little problems like this (or the sleep timer I needed) is all the training I have in scripting/programing.

I do like the backup feature. Will that also work if your tex project contains subfolders for pictures and / or chapters? And how about an upload feature to ones google account ;-)

Offline

#8 2010-10-23 16:31:19

jmbarnes
#! Junkie
Registered: 2009-05-04
Posts: 250

Re: For chronic LaTeX compilers: a script

By default the backup will backup everything in the directory you're working from EXCEPT the backups folder. That means it will descend into any subfolders for pictures and text. If some of these don't change very often (e.g. pictures) you can exclude them by adding the option "--exclude=/PATH/TO/EXCLUDE" to the tar command.

I'm not sure the easiest way to implement uploading -- would welcome comments if anyone has a tool they'd suggest. I imagine a (s)ftp transfer would be pretty easy to do. (Maybe just of the .tar file to keep it easy.)


IRC: PizzaAndWine     Script bits: Incremental Backup | Sleep Timer

Offline

#9 2010-10-23 16:54:47

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: For chronic LaTeX compilers: a script

@uploading to server:

ftp -u user@server:/sub/folder local_file
tar -cj local_stuff | ssh user@server 'tar -xj -C remote_folder'

@errors in the script:
i think i found and corrected them. (please try and report back, sorry i do not have a latex project at the moment to do the full testing myself)

@getopts:
a tried to implement some options but this might be buggy. so handle the version with getopts with care.

@checking vars:
if you want to executed a command the best i know is

if [[ -z $var ]]

if you want to use the value of the var try this function (it will work with the var "1" which is the first argument handed to the function)

myfunc(){
echo "The first argument is ${1:-not present}."
}
myfunc
myfunc hello

cheers
luc

Offline

#10 2010-10-23 17:52:03

jmbarnes
#! Junkie
Registered: 2009-05-04
Posts: 250

Re: For chronic LaTeX compilers: a script

Luc,

All super helpful. I'm not quite as savy with bash as you -- so am unclear at precisely what some of you syntax does. Unfortunately I get errors that I'm not quite able to fix in version 807.

I've incorporated some of your moves (getting rid of unnecessary variables) and used your getopts as a guide. The version below may not work with spaces (i avoid this in my own filing) but it does work otherwise. I think some problems may be had if your files don't have their extensions in their names (that is if they are thesis instead of thesis.tex).

Grab the latest w/ command line options here: http://crunchbanglinux.org/pastebin/808


IRC: PizzaAndWine     Script bits: Incremental Backup | Sleep Timer

Offline

#11 2010-10-23 19:40:54

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: For chronic LaTeX compilers: a script

i just read that you also do this script to learn something, so i hope it is all right if I explain some of my thoughts/changes to you.

learning bash (and sed):

ls | grep ".extension" | sed 's/\(.*\)\..*/\1/'
# versus
ls *.extension | sed 's/\.extension$//'

piping needs recourses so whenever you pipe from grep to sed consider using sed's search option instead i.e. sed '/search/;s/sub/stitute/' rather then grep "search"|sed 's/sub/stitute/'
and "ls *.extension" should already list only files ending with ".extension" so no search needed.
the sed command 's/\.extension$//' replaces a dot and the letters "extension" with nothing (deletes them) iff they occur at the end of a line (indicated by "$")

test_func () {
    read -p "give input  " a
    if [[ $a =~ y|Y|(Yes)|(yes)|(YES) ]] ; then
        echo "\"$a\" is a GOOD answer."
    elif [[ $a =~ n|N|(No)|(no)|(NO) ]] ; then
        echo "\"$a\" is a BAD answer."
    else
        echo "\"$a\" is an answer i do not know how to handle."
    fi
}
test_func
# you can read the manpage for regular expression:
man re_format

(to play and learn smile )
"[[ ]]" has the advantage of being a bash feature and not a program over "[ ]" (and you do not have to use quotes (that much?) with the double brackets)
"=~" is the regular expression matching operator for "[[ ]]"
"(text1)|(text2)|a|b" is a regular expression that matches "a", "b", "text1" and "text2"
("elif" should be self explaining)


filenames with spaces or without extension:
honestly i have no idea how to solve either of these issues (especially not for the potentially missing ".tex"). maybe i will look into the select stuff tomorrow.

and there is one more thing i wanted to ask you: on line 80 you have "nice tar ... " what does the nice do (or why do you use it?) i have never seen it before.

luc

Offline

#12 2010-10-24 12:23:34

Erik
#! CrunchBanger
From: Fort Worth, TX
Registered: 2009-03-28
Posts: 176

Re: For chronic LaTeX compilers: a script

Hmm for what it's worth, I think uploading to a google account is not really an option.

First, I'm not sure if you can use google's command line program in a script (as it opens its own command line) and second because, for know, it does not allow you to upload non-google-doc formats:

Note: Users with a Google Apps Premium account can use an additional option: --no-convert. This will let you upload arbitrary filetypes to Docs, like you can through the web interface. Unfortunately, this is not enabled for regular users.

Too bad, would be a nice option.

Offline

#13 2010-10-24 13:20:01

luc
#! Die Hard
From: Munich, Germany
Registered: 2010-03-21
Posts: 561

Re: For chronic LaTeX compilers: a script

How to use the "select" builtin on filenames with spaces:
i did some test in a terminal and came up with a loop to set an array. then the array can be handed to the "select" builtin and quoted to work with the spaces. It looks like this:

#first we go to a dir with files that contain spaces:
cd /dir/with/spaced/files
# now we use a loop to set the array:
i=0
for var in * ; do
  files[$i]=$var
  ((++i))
done
# now we select from the array
unset var
select var in "${files[@]}" ; do
  echo -n "You selected \"$var\" "
  if [[ -e $var ]] ; then
    echo "and it exists!"
  else
    echo "but it does not seem to exist."
  fi
  break
done

This has worked for me in bash 3.2.48

have a nice WE
luc

Offline

Board footer

Powered by FluxBB

Copyright © 2012 CrunchBang Linux.
Proudly powered by Debian. Hosted by Linode.
Debian is a registered trademark of Software in the Public Interest, Inc.

Debian Logo