Install latex with Tinytex
Tags: linux, markdown
Published: 2021-05-10 06:11:38

### Installation

[Tinytex](https://yihui.org/tinytex/) is a relative new Latex distribution by Yihui Xie from the R community. The distribution is tiny, less than 100mb, which is a great alternative to TexLive and MikTex for basic users. I this post, I will show how to install Tinytex as a standalone package independent of R for more general latex workflow.

To install Tinytex on a Linux distribution, run the following command to download and install Tinytex, as indicated in the website:
```{sh}
wget -qO- "https://yihui.org/tinytex/install-bin-unix.sh" | sh
```
This will install the application in `$HOME/.Tinytex` and add a bunch of symlinks in `$HOME/bin`:
```
random ❱ ls ~/bin
afm2tfm      extractbb     kpsereadlink     mf-nowin  pktogf          updmap
bibtex       fmtutil       kpsestat         mft       pktype          updmap-sys
dvilualatex  fmtutil-sys   kpsewhich        mktexfmt  rungs           updmap-user
dviluatex    fmtutil-user  latex            mktexlsr  simpdftex       xdvipdfmx
dvipdfm      gftodvi       latexmk          mktexmf   teckit_compile  xelatex
dvipdfmx     gftopk        luahbtex         mktexpk   tex             xetex
dvipdft      gftype        lualatex         mktextfm  texhash
dvips        inimf         luaotfload-tool  pdfetex   texlua
ebb          initex        luatex           pdflatex  texluac
etex         kpseaccess    mf               pdftex    tlmgr
``` 
Then, you can run any of the above programs in the terminal to see if they are in the PATH. If not, you need to add `$HOME/bin` to the PATH. You can add the following line:
```{sh}
export PATH="$PATH:$HOME/bin"
```
to one of your config file:`~/.profile`, `~/.bashrc`, etc. This will append `bin` folder to your PATH permanently.

Evaluate the file to update the change:
```{sh}
~ ❱ source ~/.profile
```

Now you should be able to run the programs in `bin`:

```
~ ❱ pdflatex
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=pdflatex)
 restricted \write18 enabled.
**^C
```

### Test installation with pdflatex

Now you can compile pdf from `tex` files with `pdflatex`.

You can find a sample tex file from the `$HOME/.Tinytex` folder, the original sample by Leslie Lamport that comes in every tex distribution.
```{sh}
.TinyTeX ❱ find . -name "sample2e.tex"
./texmf-dist/tex/latex/base/sample2e.tex
```
Copy this file to desired folder, I use folder named `random`:
```{sh}
.TinyTeX ❱ cp $(!!) ../
cp $(find . -name "sample2e.tex") ../random
```
Navigate to the folder that contains the `tex` file we just copied, and run `pdflatex`, you will enter into an interactive mode that ask for filename to convert. 

Enter the filename: `sample2e.tex`, the program will compile and output a pdf file from the `tex`.
```
random ❱ pdflatex
This is pdfTeX, Version 3.141592653-2.6-1.40.22 (TeX Live 2021) (preloaded format=pdflatex)
 restricted \write18 enabled.
**sample2e.tex
(./sample2e.tex
LaTeX2e <2020-10-01> patch level 4
L3 programming layer <2021-05-07>
(/home/zach/.TinyTeX/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/home/zach/.TinyTeX/texmf-dist/tex/latex/base/size10.clo))
(/home/zach/.TinyTeX/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def)
No file sample2e.aux.
(/home/zach/.TinyTeX/texmf-dist/tex/latex/base/omscmr.fd) [1{/home/zach/.TinyTe
X/texmf-var/fonts/map/pdftex/updmap/pdftex.map}] [2] [3] (./sample2e.aux) ) </h
ome/zach/.TinyTeX/texmf-var/fonts/pk/ljfour/jknappen/ec/tcrm1000.600pk></home/z
ach/.TinyTeX/texmf-dist/fonts/type1/public/amsfonts/cm/cmbx12.pfb></home/zach/.
TinyTeX/texmf-dist/fonts/type1/public/amsfonts/cm/cmex10.pfb></home/zach/.TinyT
eX/texmf-dist/fonts/type1/public/amsfonts/cm/cmmi10.pfb></home/zach/.TinyTeX/te
xmf-dist/fonts/type1/public/amsfonts/cm/cmmi7.pfb></home/zach/.TinyTeX/texmf-di
st/fonts/type1/public/amsfonts/cm/cmr10.pfb></home/zach/.TinyTeX/texmf-dist/fon
ts/type1/public/amsfonts/cm/cmr12.pfb></home/zach/.TinyTeX/texmf-dist/fonts/typ
e1/public/amsfonts/cm/cmr17.pfb></home/zach/.TinyTeX/texmf-dist/fonts/type1/pub
lic/amsfonts/cm/cmr6.pfb></home/zach/.TinyTeX/texmf-dist/fonts/type1/public/ams
fonts/cm/cmr7.pfb></home/zach/.TinyTeX/texmf-dist/fonts/type1/public/amsfonts/c
m/cmr8.pfb></home/zach/.TinyTeX/texmf-dist/fonts/type1/public/amsfonts/cm/cmsy1
0.pfb></home/zach/.TinyTeX/texmf-dist/fonts/type1/public/amsfonts/cm/cmsy7.pfb>
</home/zach/.TinyTeX/texmf-dist/fonts/type1/public/amsfonts/cm/cmti10.pfb>
Output written on sample2e.pdf (3 pages, 137025 bytes).
Transcript written on sample2e.log.
random ❱ ls
sample2e.aux  sample2e.log  sample2e.pdf  sample2e.tex
```
Now you should be use any editor with tex integration to run and compile latex!

### Integrate Tinytex with R

After set up the distribution. You can also let R use the package. To do so, you need to install `tinytex` package in R. The package should automatically recognize your tinytex distribution. To verify, use

```
#is tiny tex installed
tinytex::is_tinytex()
#verify its directory
tinytex::tinytex_root()
```
as described [here](https://community.rstudio.com/t/integrate-standalone-installation-of-tinytex-with-r/104299/2?u=kabocha-porter). Note that `tinytex` package does not install the latex distribution automatically. You need to run `tinytex::install_tinytex()` if you need a fresh installation.

Further steps:

1. Link tinytex to your existing package manager so that it will recognize the various packages that is included in tinytex instead of download new ones if your future packages depend on it