Tips for TeX

## To begin with
I want to summarize various tips for writing a paper with TeX. This article is written by English because of enhancing my English writing skill. I would be glad if you teach me mistakes in my article (e.g. English grammar or contents) or tell me your recommended packages/commands. This article is sometimes added or fixed. An environment is supposed to Ubuntu.


## How to install
You can install Japanese version texlive by following processes.

```
sudo apt-get update
sudo apt-get install texlive-lang-cjk texlive-fonts-recommended texlive-fonts-extra texlive-extra-utils
```

##  Compile the TeX code

1. Make TeX file

2. `uplatex filename`
Generate typesetting file. This file is called DVI(device independent) file.

3. `dvipdfmx filename`
Generate PDF(portable document format) file.
When you use `ptex2pdf -u -l filename` command, you can also generate DVI and PDF files at the same time.

4. `gnome-open filename.pdf`
Open a PDF file.

### Automatic compile
These processes can be replaced into `latexmk` command.
This command is really useful because we can automatically compile our document.

```
latexmk -pdfdvi -pvc filename.tex
```



## Initial code
- In case of jarticle, you use `\documentclass{ujarticle}`.
- In case of jreport, you use `\documentclass{ujreport}`.
- In case of jbook, you use `\documentclass{ujbook}`.

## Tips for TeX command

### Option on figure/table
Basically, it's good that a option on figures or table is `[tp]` or `[tpb]`.
(But this is only my opinion and it depends on journals).

### Numerical expressions
In case of one numerical expression, we should use `equation` environment. In case of multiple numerical expression, we should use `align` environment. `eqnarray` environment is deprecated.

### Roman and Bold commands
`\rm` command is deprecated. Instead of this command, we should use `\mathrm` or `\textrm` command. `\bf` command is also same as above.

### Double-quote
You use **back-quote** in front of the term and **single-quote** behind the term. 

```:TeX-code
``abc''
```

### Range
You use **endash** when you show any range.

```:TeX-code
100--500
```

### Unique noun in bibliography
When a title of bibliography includes unique nouns, you have to put braces around to the nouns.

```:bibTeX-code
@ARTICLE{bib-id,
  ...
  title = {Image segmentation using {CNN}},
  ...
}
```

## Set style files
1. Make a directory in the `/usr/share/texlive/texmf-dist/tex/latex/` directory.
2. Put the `*.sty` file in the directory.
3. Execute the command, `mktexlsr`.

```
cd /usr/share/texlive/texmf-dist/tex/latex/
sudo mkdir algorithm
sudo mv ***/algorithm.sty ./algorithm/
sudo mktexlsr
```

BibTeX file should be put in the directory, `/usr/share/texlive/texmf-dist/bibtex/bib/`.

## Recommended style files

- [flushend.sty](http://lin12.triumf.ca/text/LaTeXstyles/flushend.sty)
This file is useful to align in the final page of a paper in case of 2-columns format.

- [nidanfloat.sty](https://github.com/texjporg/nidanfloat/blob/master/nidanfloat.sty)
This file is useful to arrange figures nicely in case of 2-column format.

<!--
- [subfigure.sty](https://engineering.purdue.edu/~mark/puthesis/missing-files/subfigure.sty)
This file is useful to arrange multiple images as a figure.
**I use this package but subfigure could have been deprecation...(?)**
-->

### References
[packages and commands of deprecation](https://ichiro-maruta.blogspot.com/2013/03/latex.html) (Japanese article)


## About figure
It is a tiresome work to make or arrange figures nicely. I summarized tips for handling figures below. Basically, it's desirable that a option on figures or table is `[tp]` or `[tpb]` (But this is only my opinion and it depends on journals).

### My recommended packages
#### nidanfloat
How to download this package is written in "[Recommended style files](##Recommended style files)."

#### subcaption
This package is useful when you'd like to arrange multiple images as a figure. You have to memorize an existing caption format temporally because this package break the format. And then you can restore the format again. If you need to adjust an interval between image and caption, you can use `skip` option as follows. The default value of `skip` option is **6pt**. You write a code as follows to do above processes.

```:TeX-code
\makeatletter
\let\MYcaption\@makecaption
\makeatother

\usepackage[skip=1pt]{subcaption}

\makeatletter
\let\@makecaption\MYcaption
\makeatother
```


### Extensions of images

It is desirable that a extension of the image is `pdf` or `eps`. This is because their extensions are vector format. Vector format is constancy on scaling. Vector format is also easy to rework a image because this image consists of group of parts. When you use some software(e.g. [Inkscape](https://inkscape.org/)), you can rework PDF images easily.

### In case of PNG
If you use PNG images in a paper, you have to be careful about resolution of images.
Generally, you have to use images more than **300 dpi** in many journals.

```
mogrify -trim *.png # if you remove unnecessary spaces
extractbb *.extension
```

### In case of PDF
I recommend to use PDF vector images.
In case of using PDF, it is unnecessary to be careful about resolution of images. 

```
pdfcrop *.pdf # if you remove unnecessary spaces
```

You can write a TeX code as follows

```:TeX-code
\usepackage[dvipdfmx]{graphicx}
\graphicspath{{./images/}}

\begin{document}
\includegraphics[width=**cm]{image.jpg}
\end{document}
```

### Tips for compression of images

#### A little reduction of size in case of PDF images
You need to convert from `pdf` to `ps` extension and then reverse from `ps` to `pdf` extension.

`pdf2ps` command is useful to convert from pdf to ps extension.

```
pdf2ps original.pdf
```

`ps2pdf` command is useful to reverse from ps to pdf extension. `dPDFSETTING` option has some choices: `screen, ebook, printer, prepress` in the order of high resolution. This option also has `default` but it's deprecated.

```
ps2pdf -dPDFSETTINGS=/ebook original.ps optimize.pdf
```

The above setups are the same as making a shell-file as bellow.

```
for i in *.pdf; do
  pdf2ps $i
  f=`'echo $i | sed 's/\.[^\.]*$//'`
  ps2pdf -dPDFSETTINGS=/ebook ${f}.ps ${f}-opt.pdf
done
```

Empirically, you have no choice but to convert to PNG when size of a file is still large even if you use ebook.

#### Major reduction of size in case of PDF images
You need to convert from pdf to png extension. `-r` option has a role of adjusting DPI.

```
pdftoppm -png -r 100 original.pdf optimize.pdf
```

The above setups are the same as making a shell-file as bellow.

```
for i in *.pdf; do
  f=`'echo $i | sed 's/\.[^\.]*$//'`
  pdftoppm -png -r 100 $i ${f}-opt
done
```

#### References
[pdftoppm command](https://www.linux.com/tutorials/creating-pdf-files-ps2pdf/)
[ps2pdf command](https://www.atmarkit.co.jp/ait/articles/1903/08/news039.html) (Japanese article)

## Compress PDF documents
Use Ghostscript (gs) to compress a large PDF file.
The software has some options to choose the compression ratio.
- screen (72 dpi = screen only)
- ebook (150 dpi)
- printer (300 dpi)
- prepress (300 dpi with color preserving)
- default (almost identical to screen)


```
gs -sDevice=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed_output.pdf ./original_huge_file.pdf
```

## Useful packages in all genres
- [fancyhdr](https://ja.overleaf.com/learn/latex/Headers_and_footers)
Freely customize the header and footer in a document


## To close
That's all.
Thank you for reading :)