0

I want to add a bash command that does the following:

Currently, I need to run this:

$ pdflatex <filename>.tex | open <filename>.pdf

Is there any way to convert that into something like

$ complatex <filename>

and have both commands executed?

Melebius
  • 11,431
  • 9
  • 52
  • 78
  • 3
    is {} a syntax in find command? – mja May 06 '18 at 01:12
  • 5
    I'm skeptical about your premise since (a) pdflatex doesn't usually write to stdout (b) at least on Linux systems open doesn't usually open files and (c) even if it did, there would be no reason to pipe to its stdin; I'd expect something more like pdflatex somefile.tex ; xdg-open somefile.pdf – steeldriver May 06 '18 at 01:21
  • The question is not totally clear, but I think you're looking for this: https://askubuntu.com/a/539293/301745 – wjandrea May 06 '18 at 03:58
  • 1
    I suggest to use a function. See help function. – Cyrus May 06 '18 at 07:04
  • Thank you for your responses! To address your concerns:
    • I used {} as a placeholder for a name. Nothing fancy.
    • pdflatex doesn't write to stdout, but it will compile the pdf into the directory, so then, right after I compile it, I want to grab that compiled pdf and display it on my computer.
    • open opens files in macos. The actual system I use to open a file is outside the premise of this question, so don't worry about that :)
    • To answer the other question, user$ pdflatex main.tex | open main.pdf works perfectly fine in my computer :D
    – Landmaster May 06 '18 at 16:02

1 Answers1

3

How about this for a start:

complatex () 
{ 
    if [[ -n $1 ]]; then
        pdflatex -output-directory $(dirname "$1") "$1" &&
        xdg-open "${1%%.tex}.pdf"
    else
        for i in *.tex; do
            if [[ ! -f ${i%%.tex}.pdf ]]; then
                pdflatex "$i" &&
                xdg-open "${i%%.tex}.pdf"
            fi
        done
    fi
}

Oneline version:

complatex(){ if [[ $1 ]]; then pdflatex -output-directory $(dirname "$1") "$1" && xdg-open "${1%%.tex}.pdf"; else for i in *.tex; do if [[ ! -f ${i%%.tex}.pdf ]]; then pdflatex "$i" && xdg-open "${i%%.tex}.pdf"; fi; done; fi ;}

This function tests for an argument, if there is one it just runs pdflatex saving the output files in the argument's directory (instead of the current one) and opens the output .pdf in the default PDF viewer. If you call it without an argument, it goes through every .tex file in the current directory, tests whether there's a .pdf with the same name and only if not does the same as above.

To make the complatex command available on your system, just copy one of the two versions from above into either your ~/.bash_aliases (create it if necessary) or your ~/.bashrc file and open up a new terminal or source the changed file in an existing one with e.g. source ~/.bash_aliases.

Example run

$ tree -A --noreport
.
├── dummy.pdf
├── dummy.tex
├── other\ dir
└── test.tex
$ complatex test.tex &>/dev/null  # opens test.pdf in PDF viewer
$ tree -A --noreport
.
├── dummy.pdf
├── dummy.tex
├── other\ dir
├── test.aux
├── test.log
├── test.pdf
└── test.tex
$ rm -f test.!(tex)  # removes the output files that were just created
$ cd other\ dir/
$ complatex ../test.tex &>/dev/null  # opens test.pdf in PDF viewer
$ ls  # other dir stays empty
$ cd ..
$ tree -A --noreport
.
├── dummy.pdf
├── dummy.tex
├── other\ dir
├── test.aux
├── test.log
├── test.pdf
└── test.tex
$ rm -f test.!(tex)  # removes the output files that were just created
$ complatex &>/dev/null  # opens test.pdf in PDF viewer, doesn't process dummy.tex as there's a dummy.pdf already
$ tree -A --noreport
.
├── dummy.pdf
├── dummy.tex
├── other\ dir
├── test.aux
├── test.log
├── test.pdf
└── test.tex
dessert
  • 39,982