Write a paper with latex

Last update:

Install Texlive

Install the latex environment using the following commands.

  1. sudo apt install texlive-full

This is what all you need. Do not care about disk space although it consumes around 8 GB. After installed, we should be able to compile the below source using the command platex test.tex, which assumes the source file name is test.tex, and its contents is as shown below with character encoding of utf-8.

\documentclass{jsarticle}

\begin{document}
this is a test document
\end{document}

Then, we should be able to observes the generated file named test.dvi. Now we can generate a PDF using dvipdfmx test.dvi.

See instruction movie

See a instruction movie, like this , https://www.youtube.com/watch?v=FcVP3gGUtGI&t=1105s. Learn an essential image to latex through a video. I think this way is the most comfortable for beginners. Please don't give up at this stage.

Understand the usage of macro

Macro is the key concept to create a semantic structure in a latex source file. Never write a mathematical formula by hard writing, but use a macro. By seeing such this page, https://en.wikibooks.org/wiki/LaTeX/Macros, you can learn how to use a macro.

Establish a script to compile

Latex needs repeated compilation to resolve the cross references. and after that, it also needs to convert to pdf usually, such for preparing the document of the major international conference, by dvipdfm. So we should prepare a script to automate this process. Here is my solution written by Python3.

import subprocess
import sys
import os

print("started textopdf v1.2")

if len(sys.argv) < 2:
    print("please set a input latex filename as the second argument")
    quit()

input_path = sys.argv[1]

name,ext = os.path.splitext(input_path)

for ex in [".aux", ".toc", ".log", ".dvi"]:
    try:
        os.remove(name + ex)
    except OSError:
        pass

path11 = os.path.realpath(os.path.dirname(sys.argv[0]))
os.path.dirname(path11)

subprocess.call(["platex", name + ".tex"])
subprocess.call(["platex", name + ".tex"])
subprocess.call(["platex", name + ".tex"])

print("\n\n***************\nLaunching dvipdfmx\n************\n\n")
subprocess.call(["dvipdfmx", name + ".dvi"])

print("finished textopdf")
                

We can make a alias to run this script as below at the end of .bashrc

alias textopdf="python3 /path/to/the/above/script"

This script can be used by the command textopdf path/to/source.tex with the above alias.

How to change a text color in the document of latex with color stack

There is a concept named color stack in the implementation of dvipdfm, which used by the back end of latex's output. You can change the text (math text) color as following example .

First, define a macro. \newcommand{\XA}{\begingroup \color{red}} \newcommand{\AX}{\endgroup} And change the color like this, \XA{}this text will be changed its color.\AX{}

I suppose this might be a ultimate way to change text color in Latex. You might come up with the following way, however, you may get an error message 'color stack overflow' when you convert a dvi file to a pdf using dvipdfm

\color{red}this text will be changed its color.\color{black}

That is, there is the limit to the number of colors that was used. Additionally, the above example cannot deal with a nested structure as below. The text after blue block will be black. namely, you have to keep in it your mind that you must choose the appropriate color at the end of colored blocks.

\color{red}this text will be \color{blue}blue!\color{black}changed its color.\color{black}

Embedding fonts to PDF

We can embed japanese fonts in PDF when it is generated by dvipdfmx by the setting using the following command .

kanji-config-updmap ipa

After executing this command, we can generate a PDF which all fonts are embedded by dvipdfmx. There is no additional option to dvipdfmx command needed.


References

  1. https://texwiki.texjp.org/?special%20%E5%91%BD%E4%BB%A4
  2. https://texwiki.texjp.org/?TeXとフォント