Hacker Newsnew | past | comments | ask | show | jobs | submit | tls-kn's commentslogin

Script I quickly wrote that automatically compiles my tex files on change, and reloads my pdf viewer (mupdf in this particular case). This was written for OpenBSD (TeXstudio wasn't available, and I ended up liking this editor+mupdf approach even more), so I don't know if it perfectly translates to other OSs.

    #!/bin/sh
    
    pdf_viewer="mupdf";
    latex_cmd="pdflatex -interaction=nonstopmode"
    
    if [[ $# -eq 0 ]]; then
        print "No arguments: filename required"
        exit
    fi
    
    filename=$1;
    pdfname=${filename%%.*}.pdf
    
    # inital compilation to make sure a pdf file exists
    ${latex_cmd} ${filename};
    
    ${pdf_viewer} ${pdfname} &
    
    # get pid of the pdf viewer
    pdf_viewer_pid=$!;
    
    while true; do
        # as long as the pdf viewer is open, continue operation, if it gets closed,
        # end script
        if kill -0 "${pdf_viewer_pid}" 2>/dev/null; then
            if [[ ${filename} -nt ${pdfname} ]]; then
                ${latex_cmd} ${filename};
    
                # reload pdf file, only works with mupdf
                kill -HUP ${pdf_viewer_pid};
                touch $pdfname
            fi
            sleep 1;
        else
            exit 0;
        fi
    done;


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: