Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ziu1986 bash script #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,41 @@ Thanks to Inger Helene Karset for sharing and as far as I am aware, it was Andr
## Usage and compilation of template
This template can be used with overleaf or your favorite LateX editor. Note that overleaf has an integration with GitHub, so it can be synced to GitHub (a premium version of Overleaf is needed for this).

If you wish to compile with the terminal locally you must run the following commands in the given order
If you wish to compile directly with the terminal locally you must run the following commands in the given order
```shell
$ pdflatex main.tex
$ bibtex main
$ pdflatex main.tex
$ pdflatex main.tex
```

You can also use the bash script called 'make'. Execute it by typing
```shell
./make

```
in your shell. Follow the instructions on screen.

- The script compiles the latex document which can be seperated into a main document and dependend sections located in different files.
- All figures are expected to be located in a directory called figures_src.
- Original graphics in vector formates will be converted into pdf and moved to figures directory.
- Graphics in pdf or bitmap formats will be linked into a directory figures.
- The compiled document will automatically open in okular if installed else evince will be called.
- A short summary taken from the tex log files is shown on screen, counting the occurance of box under and overflows and missing references.
- There is also an option to compress the file for pdf-a usage.

``` shell
./make compress
```

- You can clean up the directory for latex compilation files
``` shell
./make clean
```

Possible comandline options:
"open" "clean" "check" "pic" "compress" "copy" "review"


## Contribution
Suggestions for any improvements and pull requests are highly welcomed!
File renamed without changes
302 changes: 302 additions & 0 deletions make
Original file line number Diff line number Diff line change
@@ -0,0 +1,302 @@
#!/bin/bash
# Script to compile latex documents which has to be seperated in one main document and dependend sections in different files.
# Figures are expected in a directory pictures_src.
# Original graphics in vector formates will be converted into pdf and moved to pictures directory.
# Graphics in pdf or bitmap formats will be linked into a directory pictures.
# The compiled document will automatically opened with okular if installed.
# There is also an option to compress the file.
# Function declarations
function findMain {
if([ ! $file ]); then
if([ ! ${1} ]); then
echo "Provide base file name or press ENTER."
read file
echo $file
if [[ -z ${file} ]]; then
echo "Assuming file:"
file=`grep -lr --include="*.tex" "begin{document}" .`
fi
fi
fi
if([ ! -e $file ]); then
echo "$File does not exist!"
exit 1
fi
echo "==> $file"
file=`basename $file .tex `
bib_file=`find . -name *.bib`
}

function findUpToDate {
# if there are no changes to any .tex file or to .bib
# script will exit
changes=$1;
if [[ $changes -gt 0 ]]; then
rm $file.${clear_suff[0]}
fi

for i in `find . -name \*.${suffix[0]} | xargs`;
do
if [[ ${i} -ot $file.${suffix[4]} ]]; then
echo "No changes to `basename ${i}`"
else
changes=$(($changes + 1))
fi
done

if [[ $file.${suffix[2]} -ot $file.${suffix[4]} ]]; then
echo "No changes to $file.${suffix[2]}."
else
changes=$(($changes + 1))
rm ${file}.${suffix}[4]
fi
if [[ $changes -eq 0 ]]; then
echo "Nothing to do."
if [[ ${1} -eq ${options[0]} ]]; then
echo "Will open $file.${suffix[3]}."
openPdf
fi
exit
fi
}

function makeBib {
if ([ `grep -c --exclude=*.sh "LaTeX Warning: There were undefined references." $file.${suffix[1]}` > 0 ] || [ -e $file.${suffix[2]} ]) ; then
if [[ `grep -c --exclude=*.sh "biber" $file.${suffix[0]}`> 0 ]]; then
biber $file
pdflatex $directory/$file.${suffix[0]}
else
bibtex $file.${suffix[4]}
fi
pdflatex $directory/$file.${suffix[0]}
fi
if ([ `grep -c --exclude=*.sh "LaTeX Warning: Label(s) may have changed." $file.${suffix[1]}` -gt 0 ]); then

pdflatex $directory/$file.${suffix[0]}

fi
}

function makeLineno {
if ([ `grep -c --exclude=*.sh "Package lineno Warning: Linenumber reference failed" $file.${suffix[1]}` -gt 0 ] || [ -e $file.${suffix[2]} ]) ; then
pdflatex $directory/$file.${suffix[0]}
fi
}

function makePic {
targetPath=pictures
path=pictures_src
if([ -d $path ]); then
if([ ! -d $targetPath ]); then
mkdir $targetPath
echo "Created directory $targetPath."
fi
fi
updatedPics=0;
for i in `ls $path | xargs`; do
srcFile=${i}
if ([ "$srcFile" != "${srcFile/%eps}" ]); then
# Deal with eps
targetFile=$targetPath/`basename $srcFile .eps`.pdf
if [[ -e $targetFile ]]; then
if [ $(stat -c '%Z' $path/$srcFile) -gt $(stat -c '%Z' $targetFile) ]; then
rm $targetFile
fi
fi
if [[ ! -e $targetFile ]]; then
#echo $targetFile
`epstopdf $path/$srcFile`
echo "Converted $srcFile to eps."
mv $path/`basename $targetFile` $targetPath
echo "Moved `basename $targetFile` to $targetPath."
updatedPics=$(($updatedPics + 1))
fi
fi
if ([ "$srcFile" != "${srcFile/%svg}" ]); then
# Deal with svg
targetFile=$targetPath/`basename $srcFile .svg`.pdf
if [[ -e $targetFile ]]; then
if [ $(stat -c '%Z' $path/$srcFile) -gt $(stat -c '%Z' $targetFile) ]; then
rm $targetFile
fi
fi
if [[ ! -e $targetFile ]]; then
`inkscape -z --file=$path/$srcFile --export-pdf=$targetFile`
echo "Created `basename $targetFile`"
updatedPics=$(($updatedPics + 1))
fi
else
# Deal with pdf and png...
targetFile=$targetPath/$srcFile
if( [ ! -e $targetFile ] || [ $(stat -c '%Z' $path/$srcFile) -gt $(stat -c '%Z' $targetFile) ]); then
# Don't know why, but link needs to be realative.
cd $targetPath
ln -s "../$path/$srcFile" $srcFile
echo "Created symbolic link for $srcFile in $targetPath."
cd ..
updatedPics=$(($updatedPics + 1))
fi
fi
done

echo "Pictures updated: $updatedPics"
}

function makeClean {
echo "Clean directory."

for i in ${clear_suff[@]}; do
echo "Remove ${file}.${i}."
rm *.${i}
done
if([ -d pictures ]); then
rm -r pictures
fi
exit
}

function openPdf {
if [ `grep -c "Output written on." $file.${suffix[1]}` -eq 0 ]; then
echo "Exit."
#exit
#not nice but seems to do the job
elif ([ ! `ps -f | grep -c $file.${suffix[3]}` -gt 1 ]); then
if [ -z `which okular` ]; then
evince $file.${suffix[3]}
else
okular $file.${suffix[3]}
fi
else
echo "$file.${suffix[3]} is already running."
fi
}

function spellCheck {
for i in `find . -name \*.${suffix[0]} | xargs`;
do
if [[ ${1} = "de" ]]; then
aspell -t -x -c --lang de_DE-neu ${i}
else
aspell -t -x -c ${i}
fi
done
exit
}

function compress {
compressedFile="$file-compressed.${suffix[3]}"
if ([ -f $file.${suffix[3]} ]); then
`gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=$compressedFile $file.${suffix[3]}`
fi
}

function getWarnings {
WARNINGS=`grep -c "[W/w]arning" $file.${suffix[1]}`
echo "++++"
echo "There occurred $WARNINGS warning(s):"
if ([ $WARNINGS -le 10 ]); then
grep -s -n --color "[W/w]arning" $file.${suffix[1]}
fi
echo "++++"
}

function getErrors {
echo "++++"
echo "There occurred `grep -c "[E/e]rror" $file.${suffix[1]}` error(s):"
grep -s -n --color "[E/e]rror" $file.${suffix[1]}
echo "++++"
}

function getOverFlow {
OVERWARNINGS=`grep -c "[o,O]verfull" $file.${suffix[1]}`
echo "++++"
echo "There occurred $OVERWARNINGS box overflow(s)."
if ([ $OVERWARNINGS -le 10 ]); then
grep -s -n --color "[o,O]verfull" $file.${suffix[1]}
fi
echo "++++"
}

function getUnderFlow {
UNDERWARNINGS=`grep -c "[u,U]nderfull" $file.${suffix[1]}`
echo "++++"
echo "There occurred $UNDERWARNINGS box underflow(s)."
if ([ $UNDERWARNINGS -le 10 ]); then
grep -s -n --color "[u,U]nderfull" $file.${suffix[1]}
fi
echo "++++"
}

function makeCopy {
target=${1}
echo "++++"
echo "Copying ${file}.${suffix[3]} to given destination:"
echo "${target}"
echo "++++"
cp ${file}.${suffix[3]} ${target}
}

function makeReview {
echo "++++"
echo "Preparing abstract."
echo "Zipping figures."
echo "++++"
pdfseparate -f 1 -l 1 ${file}.${suffix[3]} ${file}_abstract.${suffix[3]}
zip figures -xi fig*
}

### MAIN ###

suffix=( tex log bib pdf aux )
pic_suff=( pdf jpg jpeg png bmp tiff pnm )
clear_suff=( aux bbl blg out log pdf toc )
directory=`pwd`
options=( "open" "clean" "check" "pic" "compress" "copy" "review")

case ${1} in
"")
findMain
makePic
findUpToDate $updatedPics
pdflatex -halt-on-error $directory/$file.${suffix[0]}
makeBib
makeLineno
getWarnings
getOverFlow
getUnderFlow
getErrors
openPdf
;;
${options[1]})
makeClean ${2}
exit
;;
${options[2]})
spellCheck ${2}
exit
;;
${options[0]})
openPdf
exit
;;
${options[3]})
makePic
exit
;;
${options[4]})
compress
exit
;;
${options[5]})
makeCopy ${2}
exit
;;
${options[6]})
makeReview
exit
;;
*)
echo "Options"
echo ${options[@]}
exit
esac
2 changes: 1 addition & 1 deletion parts/background.tex
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ \section{Second section with a figure}

\begin{figure}
\centering
\includegraphics[width=0.65\textwidth]{figurer/ship_tracks.jpg}
\includegraphics[width=0.65\textwidth]{figures/ship_tracks.jpg}
\caption{\textit{Ship tracks observed by NASA's MODIS instrument on board the Aqua satellite. Figure retrieved from \url{https://svs.gsfc.nasa.gov/3667} October 9, 2019. More information about the instrument is found in Chapter \ref{ch:metode}.}}
\label{fig:shiptracks}
\end{figure}
Expand Down