Feeds:
Posts

Document edition with LaTeX

I define myself as a $\LaTeX$ lover. I write all my documents using $\LaTeX$.

In this post I’d like to review some features about $\LaTeX$ writing and compilation. Since I’m a Linux user, and don’t have any idea on how does windows work, I’ll restrict myself to Linux OS… particularly Debian based ones, such as, Ubuntu, Mint, and so on.

Installing the Compiler

The easiest way of installing the $\LaTeX$ compiler in Debian based Linux, is through the Terminal, (you should have sudoes power)

 $sudo apt-get install texlive or if you prefer to install all the possible packages, $ sudo apt-get install texlive-full

NOTE: the full installation needs about 1GB of free pace in your HD, which is not too much by this days, however, downloading the installation packages could long couple of hours with ease.

Choose an Editor

One cannot write a $\LaTeX$ file in a Word Processor as OpenOffice or LibreOffice. Nonetheless, there are different processors which are useful for this end,

• Gedit: included in most Linux distributions
• Emacs: a very powerful processor
• Texmaker: specially designed for LaTeX, in GNOME environment
• Kile: specially designed for LaTeX, in KDE environment

and many others,

• Texila
• Texshop
• Texmacs
• et cetera

You could install one or all of them, again using the CLI, for example,

 sudo apt-get install texmaker emacs gedit-latex-plugin First LaTeX Document From now on, I’d restrict to Emacs editor (which is my favourite), the terminal for compiling the document, and evince as viewer. NOTE: In case you choose to try emacs yourself, I recommend to check some old post of mine, about an emacs error, about environments, or emacs and LaTeX. Three first lines All $\LaTeX$ document has the three very first lines which define it. \documentclass{report} \begin{document} \end{document} The first one define the type of document one’d like to write, some classes are • report: a book-like document, probably less complex • book: specially for writing books • article: for scientific articles • letter: for writing letters • beamer: for presentations (see for example this post) • currvita: for writing curricula vitae • and many more… All that is written between the begin and end document is called the body of the document, in there all chapters, sections and so on is included. A special part of the document is the area between the documentclass and the begin document. This is called preamble, and there all the special request about our document are specified. Giving some Format In order to start giving structure to our document, we need to know the kind of structures defined in $\LaTeX$, • part: for book and report classes • chapter: for book and report classes • section: for book, report and article classes • subsection: as above • subsubsection: as above • paragraph: as above • subparagraph: as above by now, we restrict ourself to these three classes, because the other are quite different. Each structure is understand as a command, thus it must be presided by the backslash and include into the body of the document, \documentclass{report} \begin{document} \chapter{Introduction} \end{document} As you might notice, the name of the chapter is enclosed by curly brackets. NOTE 1: From part to subsubsection, structures are numbered by default. If you’d like to avoid the number, use an asterisk as shown below, \documentclass{report} \begin{document} \chapter*{Introduction} \end{document} NOTE 2: A huge difference between a chapter (which I’m calling structure) and environment (such as document), is that the former ends when a new structure is given, while the later has a beginning and an end. Some Environments There are lots of different environments that might be used while writing a document, such as, • equation: for writing a single numbered equation. Use equation* for unnumbered ones. • eqnarray: for writing multiple numbered aligned equations. Use \nonumber for avoid a numbered line, \\ split the line, and && for telling where to align(the sign could be surrounding a sign). • quote: for quotations. • itemize: for unnumbered list. • enumerate: for numbered list. • figure: for adding numbered and possibly captioned figures. • table: for adding numbered and possibly captioned tables. • tabular: for constructing tables. • minipage: allows to construct a mini-paga on the document, like a post it! • and a long standing list which cannot be possible cover in a post! Compiling the Document Since $\LaTeX$ is a programming language, one necessarily has to compile the document to get a PDF or PS (human) readable file. The very fist step is to save the plain text file, the extension must be .tex, as it’s customary, I’ll call our foo.tex. Since compilation usually generates a lot of trash files, the best place to saave the plain text is in a folder dedicated for latex files. In Emacs the keys Ctrl-x Ctrl-s abbreviated by C-x C-s, saves the file… or buffer in Emacs jargon. The first time you save the file, a name must be given, ex., ~/Latex/foo.tex. NOTE: I assume that a folder Latex was created in your home folder (~). Once saved, go to the terminal and change the promt to the Latex folder,  cd ~/Latex

and compile the file with pdflatex command

 $pdflatex foo.tex Finally open the document, with evince, $ evince foo.pdf &

😀 Yeah babe!!!! That’s right… your first document written in $\LaTeX$.

Writing Equations

I’m a physicist, so I’m used to write lots of equations. I’ll explain some examples,

$$\vec{F} = m \vec{a}$$

results in

$\vec{F} = m \vec{a}$

or the famous Einstein’s relation,

$$E = m c^2$$

results in

$E = m c^2.$

More complicated examples,

$$\frac{\partial^2 }{\partial t^2}x(t) + \omega^2 x(t) = 0\;\Rightarrow\; x(t) = A\sin(\omega t)+ B\cos(\omega t)$$

results in

$\frac{\partial^2 }{\partial t^2}x(t) + \omega^2 x(t) = 0\;\Rightarrow\; x(t) = A\sin(\omega t)+ B\cos(\omega t)$

With this example we opened the gate of Greek alphabet in mathematical mode, and Calculus notation… nevertheless, there exist endless possibilities. I encourage you to check symbols-a4.

The best of the compiled text, is that numbers (of section, chapter, equations, tables, or figures) are assigned by the compiler… therefore, you don’t need to remember those damn numbers. So, How do I refer to an equation?

First, give a name to the equation, with the label command

$$E = m c^2 \label{emc2}$$

and then, call it with the ref command,

As we saw before, the mass relation (\ref{emc2}), bla bla bla...

After the compilation the precise number appears.

NOTE: The same principle work for any other structure of the document or numbered environment.

And the Title Page?

As you have notice, there’s a huge difference between WYSIWYG editors (Office-like) and WYWIWYG ones (which must be compiled). The maketitle command orders to the compiler to create a title page. The data should be included in the preamble, whilst the command form part of the document body,

\documentclass{report}
\title{My first document in \LaTeX{}}
\author{Dox Drum}
\date{\today}

\begin{document}

\maketitle

\chapter*{Introduction}

The harmonic oscillator is driven by the differential equation,
$$\frac{\partial^2 }{\partial t^2}x(t) + \omega^2 x(t) = 0\;\Rightarrow\; x(t) = A\sin(\omega t)+ B\cos(\omega t).$$

\end{document}

Listing

As we saw above, list are made with the commands itemize or enumerate, whether you want it to be numbered or not.
Ex.:

\begin{itemize}
\item This got no number
\item Neither does it!
\begin{enumerate}
\item This is number one
\item number two
\end{enumerate}
\item Another with no number
\end{itemize}

which is a numbered list inside a unnumbered one.

SAGE tip: More about Differential Forms

In a previous post we discuss the definition of the coordinated patch on a manifold, how to define differential forms, wedge them or calculate their exterior derivative… even simplify’em.

This time a zero form will be defined and a list of forms will be created… So, let’s begin!

Define a 0-form

Once created the coordinated patch and the differential forms algebra

sage: reset()
sage: var('t,x,y,z')
sage: U = CoordinatePatch((t,x,y,z))
sage: Omega = DifferentialForms(U)

A 0-form is defined as an element of $\Omega^0(U)$, but the value of the 0-form is given inside the declaration command,

sage: A = DifferentialForm(Omega, 0, exp(x*y))

I tried addition, multiplication, wedge product and exterior differentiation on 0-forms and they worked!

Of course you can combine them with forms of different degrees.

New method of defining a form

I wrote to Joris this morning… but before he was able to answer, from the documentation of the differential form package.

When one calls the generators of the differential form,

sage: Omega.gen(1)
dx

and the result is a differential form… Thus, one can assign a form as follow,

sage: A = sin(x)* Omega.gen(2)
sage: B = cos(y) * Omega.gen(0)
sage: C = sin(z) *Omega.gen(1)
sage: D = cos(y) * Omega.gen(2)

And this forms can be wedged, differentiated, et cetera. 🙂

List of forms

Finally, after discovering the above behavior I tried the following, a list of differential forms ;-), for example,

sage: pro = matrix([[A, B], [C, D]])
sage: for i in range(2):
...       for j in range(2):
...           show(pro[i,j].diff())

returns
$cos(x)dx\wedge dz$
$sin(y)dx\wedge dy$
$-cos(z)dy\wedge dz$
$-sin(y)dy\wedge dz$

This implies that somehow one can manage a series of forms by using list properties… I expect to go deeper on this subject in the future! 😀

Enjoy people!!!

Dox

SAGE tip: Convert a 2D list into a matrix

In yesterday SAGE tip, I did a “small” worksheet for calculating Riemann and Ricci tensors given a metric.

The worked example was the Schwarzschild solution,

$ds^2(g) = -\frac{dt\otimes dt}{1-\frac{2M}{r}}+\left(1-\frac{2M}{r}\right) dr\otimes dr+ r^2\left(d\theta\otimes d\theta +\sin^2(\theta) d\varphi\otimes d\varphi\right)$,

which is a vacuum solution, i.e., $R_{\mu\nu} = 0.$

When I ran the worksheet, the result of the Ricci tensor was right, but didn’t simplify to zero… So, I decide to bypass this applying the trick of matrix simplification.

But first… How do I convert a list to a matrix? Well, my first try worked!! 🙂 (wonderful!)

For the five dimensional Schwarzschild metric the Ricci tensor (find the worksheet in my webpage obtained was,

sage: Rdd = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 2*(2*M/r^2 - 1)*sin(theta)^2 + sin(theta)^2 - cos(theta)^2 - 4*M*sin(theta)^2/r^2 + 1, 0], [0, 0, 0, 0, 2*(2*M/r^2 - 1)*sin(phi)^2*sin(theta)^2 + sin(phi)^2*sin(theta)^2 - sin(phi)^2*cos(theta)^2 - 4*M*sin(phi)^2*sin(theta)^2/r^2 + sin(phi)^2]]

which would be zero.

The line,

sage:matrix(SR, Rdd).apply_map(lambda x: x.simplify_full())

 [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0] 

converts the list into a matrix, and the apply_map() with the defined lambda function simplifies the elements of the matrix.

Enjoy!

DOX

Looking on published worksheets, I found a simple code for 4th order Runge-Kutta applied to 1st order ODE here.

I took the same code and increce the number of interactions (from 2 to 100) and the array soln now was a list  of (100 x 2) data. Of course a plot is much more useful than a really long list, so the easiest way of plotting the data is adding a new line to the code,

list_plot(soln)

This command plots the data in the 1st column vs. data of the 2nd column. Additionally, If you want to save the plot (instead of showing it) use the filename option,

list_plot(soln, filename='/path/to/directory/name-of-plot.jpg')

remember that the extension could be also PDF, PNG, EPS, among others.

Thanks to omologos in the sage-dev channel of IRC

Another rather interesting way of plotting arrays  was pointed out to me by mvngu in the following code,

sage: L1 = [randint(1, 10) for i in xrange(2)]
sage: L2 = [randint(1, 10) for i in xrange(2)]
sage: L3 = [randint(1, 10) for i in xrange(2)]
sage: L4 = [randint(1, 10) for i in xrange(2)]
sage: L5 = [randint(1, 10) for i in xrange(2)]
sage: M = matrix([L1, L2, L3, L4, L5]); M
[ 1  2]
[ 1  3]
[10  6]
[ 3  8]
[ 6  1]
sage: line2d(zip(M.column(0), M.column(1)), thickness=1)


The last line does the work done

For another example see the notebook by rsael at sagenb.