Using Latex with Dropbox

Update 2010/09/05: An issue in the provided scripts has been fixed that caused the generation to fail when the argument passed was surrounded by quotes.

Lately I’ve been using Dropbox on a regular basis to sync my files online and across computers automatically. Dropbox syncs my files as soon as a file is modified, deleted or added inside the main Dropbox folder. This is great, unless some process generates files you wanted to exclude from synchronization. LaTeX, as most ‘programming’ languages do, generates additional files while compiling your .tex input files. If not told otherwise these additional files appear in the same directory as your source files and are therefore immediately pushed to Dropbox. You could, of course, delete these unnecessary files, but Dropbox would always keep a record of them.

To improve one could

  1. extend Dropbox to exclude certain directories or files based on patterns. Note the latest developer version offers a feature called “selective sync” that allows users to exclude certain directories from synchronizing with Dropbox.
  2. tell LaTeX to generate its output elsewhere.

Even if option #1 gets mature, I think it is always a good idea to separate source files from generated output. I’ve come up with a simple Windows batch file that allows you to invoke the entire LaTeX chain and keep sources and builds separate from each other.

It reads something along the lines of

:: 
:: pdflatex_full.bat
:: A batch script for compiling latex documents into pdfs through pdflatex.
::
:: Copyright (c) 2010 Christoph Heindl
:: https://cheind.wordpress.com
::
@echo off

:: no argument provided
if "%~1" == "" goto NOARGS

:: parse path provided
set master_fullpath=%~1
set master_filename=%~n1
set master_path=%~dp1
if "%master_path:~-1%"=="\" set master_path=%master_path:~0,-1%
:: configure outputs
set out_dir=%HOMEDRIVE%%HOMEPATH%\latex_output\%master_filename%
set log_file=%out_dir%\log.txt

:: assemble arguments passed
set pdf_args=^
 -include-directory="%master_path%"^
 -output-directory="%out_dir%"^
 -aux-directory="%out_dir%"^
 -interaction=nonstopmode^
 -halt-on-error^
 -quiet
set bibtex_args=^
 -include-directory="%master_path%"^
 -include-directory="%out_dir%"^
 -quiet

:: create output directory if it doesn't exist
IF EXIST "%out_dir%" GOTO GENERATE
mkdir "%out_dir%"
if %ERRORLEVEL% NEQ 0 GOTO NOCREATE

:: generate output through multiple passes
:GENERATE
echo Generating %out_dir%\%master_filename%.pdf ...
pdflatex %pdf_args% "%master_fullpath%" > "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR 
bibtex %bibtex_args% "%out_dir%\%master_filename%" >> "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR 
pdflatex %pdf_args% "%master_fullpath%" >> "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR
pdflatex %pdf_args% "%master_fullpath%" >> "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR
exit 0

:: error handler invoked if no tex file is provided
:NOARGS
echo Error no master tex file provided
pause
exit -1

:: error handler invoked if creating output directory failed
:NOCREATE
echo Failed to create output directory %out_dir%
pause
exit -1

:: error handler invoked when a single generation pass fails
:ERROR
echo Failed to generate ouput
type "%log_file%"
pause
exit %ERRORLEVEL%

To invoke it make sure you have pdflatex.exe and bibtex.exe in your lookup path and type

pdflatex_full.bat path\to\master.tex

Alternatively you can drag&drop files onto the batch script. One neat trick is to store pdflatex_full.bat in Windows SendTo folder, so you can quickly invoke it by right-clicking a .tex file.

pdflatex_full.bat will advise the latex chain to generate all output inside a folder named latex_output located directly in the current users home directory. pdflatex_full.bat can handle bibtex and mutli-file documents. The command window will automatically close if no error occurs, else it stays visible showing the cause of error. Note that error detection is performed via the executables exit code which is not supported by bibtex.exe.

If you prefer to use a latex, dvips and ps2pdf chain the following slightly modified version of pdflatex_full.bat is for you.

:: 
:: latex_full.bat
:: A batch script for compiling latex documents into pdfs using latex and dvips.
::
:: Copyright (c) 2010 Christoph Heindl
:: https://cheind.wordpress.com
::
@echo off

:: no argument provided
if "%~1" == "" goto NOARGS

:: parse path provided
set master_fullpath=%~1
set master_filename=%~n1
set master_path=%~dp1
if "%master_path:~-1%"=="\" set master_path=%master_path:~0,-1%

:: configure outputs
set out_dir=%HOMEDRIVE%%HOMEPATH%\pdflatex\%master_filename%
set log_file=%out_dir%\log.txt

:: assemble arguments passed
set latex_args=^
 -include-directory="%master_path%"^
 -output-directory="%out_dir%"^
 -aux-directory="%out_dir%"^
 -interaction=nonstopmode^
 -halt-on-error^
 -quiet
set bibtex_args=^
 -include-directory="%master_path%"^
 -include-directory="%out_dir%"^
 -quiet
set dvips_args=^
 -Ppdf^
 -o "%out_dir%\%master_filename%.ps"

:: create output directory if it doesn't exist
IF EXIST "%out_dir%" GOTO GENERATE
mkdir "%out_dir%"
if %ERRORLEVEL% NEQ 0 GOTO NOCREATE

:: generate output through multiple passes
:GENERATE
echo Generating %out_dir%\%master_filename%.pdf ...
latex %latex_args% "%master_fullpath%" > "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR 
bibtex %bibtex_args% "%out_dir%\%master_filename%" >> "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR 
latex %latex_args% "%master_fullpath%" >> "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR
latex %latex_args% "%master_fullpath%" >> "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR
latex %latex_args% "%master_fullpath%" >> "%log_file%" 2>&1
if %ERRORLEVEL% NEQ 0 GOTO ERROR
:: need to temporarily change current current directory, since dvips does not support
:: include directories.
pushd "%master_path%"
  dvips %dvips_args% "%out_dir%\%master_filename%" >> "%log_file%" 2>&1
  if %ERRORLEVEL% NEQ 0 GOTO ERROR
popd
ps2pdf "%out_dir%\%master_filename%.ps"
if %ERRORLEVEL% NEQ 0 GOTO ERROR
exit 0

:: error handler invoked if no tex file is provided
:NOARGS
echo Error no master tex file provided
pause
exit -1

:: error handler invoked if creating output directory failed
:NOCREATE
echo Failed to create output directory %out_dir%
pause
exit -1

:: error handler invoked when a single generation pass fails
:ERROR
echo Failed to generate ouput
type "%log_file%"
pause
exit %ERRORLEVEL%

Both scripts can easily be adjusted to your needs and come without warranty of any kind.

About these ads

3 thoughts on “Using Latex with Dropbox

  1. Pingback: Peters Linkschleuder – Der Schockwellenreiter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s