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
{}
a syntax infind
command? – mja May 06 '18 at 01:12pdflatex
doesn't usually write to stdout (b) at least on Linux systemsopen
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 likepdflatex somefile.tex ; xdg-open somefile.pdf
– steeldriver May 06 '18 at 01:21help function
. – Cyrus May 06 '18 at 07:04pdflatex
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 :)user$ pdflatex main.tex | open main.pdf
works perfectly fine in my computer :D