# Google docs to TeX and pdf shell script produces a blank first page, and gives me a “! LaTeX Error: Missing \begin{document}.” error

I made a shell script, Ubuntu 14.04 (taking ideas from google docs as team editor) to grab a google docs file and convert it to LaTeX, compile with LaTeXmk to PDF and then open the pdF with evince as follows:

#!/bin/bash

wget -O $2.tex "https://docs.google.com/document/export?format=txt&id=$1" &&    latexmk -pdf -f -interaction=nonstopmode $2.tex || evince$2.pdf


The first argument of the script $1 is the file sharing id of the file I want to grab the text from (I made a shareable link of the google doc and take the id from there), the second $2 is the name of the file I want to produce without the file extension.

It works okay, but for some reason produces a blank first page. It also produces lots of errors. However, I suspect that the blank first page results from this error:

! LaTeX Error: Missing \begin{document}.


My google docs document ends up producing a LaTeX file that looks like this:

\documentclass{article}
\begin{document}

I am the walrus.

\end{document}


Which is exactly how the google doc looks. So I don't know why it would complain about a missing \begin{document} when I have one in my file. Not surprisingly, having little to no programming skills, I haven't been able to figure out why it is producing output starting on the 2nd page. And so here I am, any help greatly appreciated.

• Debug the process to figure out where stuff breaks. Download the file. Then compile separately. If that fails, check with a non-downloaded file. Does compilation fail? If so, troubleshoot that issue first because that problem has nothing to do with Google Docs. If not try to get the Google Docs download to match the working test file. You need to narrow down what is going wrong in order to debug any issue like this. Even if it seems to work, a shell script you want to rely on should do some checks and give you some useful feedback when things go wrong. For now, don't script at all. – cfr Dec 28 '15 at 3:29
• If you can download the file and compile with pdflatex, troubleshoot the latexmk step. Try latexmk -pdf -verbose <filename>.tex. You don't want it to continue despite errors. You want the first error. Similarly, you don't want no interaction. You want it to stop and tell you stuff. Is a .log file produced? Anything there? Make sure that you have no strange characters in any path or file names. Stick to ASCII letters and numerals and hyphens. No spaces. Nothing strange. (Underscores can be OK but avoid for now.) – cfr Dec 28 '15 at 3:32
• Does the same with pdflatex. Thanks, I'll try your suggestions. – A Feldman Dec 28 '15 at 3:36
• So you download the file as test.tex. Then you do pdflatex test.tex and get the error about missing \begin{document}? If so, create a document from scratch with that content in your editor and call it test2.tex. Now, pdflatex it. Do you get the error? – cfr Dec 28 '15 at 3:38
• If I had to guess, I'd say there's something else in that document. It may not be visible, but it is there and it is before \begin{document}. If you've got invisible characters, TeX may still see them. – cfr Dec 28 '15 at 3:43

#!/bin/bash
wget -O $2.tex "https://docs.google.com/document/export?format=txt&id=$1";
bom-remove.sh $2.tex; latexmk -pdf -f -interaction=nonstopmode$2.tex;