Showing posts with label latex. Show all posts
Showing posts with label latex. Show all posts

2008-09-22

Python inside LaTeX (and Sage too)

I discovered recently, that it is possible to run Python scripts from LaTeX documents and use the to generate document's content. This can be used to read/convert data, generate tables and figures, do on-the-fly calculations.

How to run Python code from LaTeX:

  • download python.sty. Put it in the same directory as the LaTeX document
  • \usepackage{python}
  • Put Python code inside \begin{python}\end{python} environment. Whatever this code prints, will become part of the LaTeX document.
  • Run LaTeX with -shell-escape option (this permits running external code from LaTeX)
This is an example of the LaTeX document and the PDF produced.

And some more advanced examples are in this LaTeX file (see also the PDF). They cover symbolic calculations from inside LaTeX, plotting and variable persistence between python environments.

P.S. What's more, one can even embed the full-featured symbolic math package Sage into LaTeX. For this purpose use sagetex package. By the way, you do not need to install Sage to start using it, please check out its web-version. It is really powerful.

Links:

This post in Russian: Python внутри LaTeX (и математический пакет Sage тоже)

2008-07-15

Auto-building LaTeX documents with SCons

How many commands does it take to compile LaTeX document?
1. pdflatex mypaper.tex
2. bibtex mypaper
3. pdflatex mypaper.tex
4. pdflatex mypaper.tex
— and how many times do you have to type them when you edit an article? A lot.

Fortunately, there is a simple cure. A SCons build system. Install it and create a new SConstruct file where the LaTeX document lives, with a contents like this:

PDF(target = 'mypaper.pdf', source = 'mypaper.tex')
Then just run
$ scons
And SCons will automatically run bibtex if you use it, it will run PDFLaTeX as many times as it needs (SCons is smart enough to read the log file of the LaTeX), SCons will remember MD5 sum of the source file and will avoid re-compiling the PDF if the source file did not change. Very convenient!

I also prefer to re-build the PDF whenever I change any of the figures. Given that my figures are in imgs/ subfolder and all are either *.pdf or *.png files, I can create a list of “source” files using SCons' Glob function. This is what SContstruct then looks like:

src_list = [ 'mypaper.tex', Glob('imgs/*.pdf'), Glob('imgs/*.png') ]
PDF(target = 'mypaper.pdf', source = src_list)
Of cource, one can still use make to compile LaTeX documents. This is a good Makefile for a start.

This post in Russian: Автоматическая компиляция документов LaTeX

2008-04-15

Various LaTeX tips

I decided to bring together LaTeX tips from my other blog.

How to change line spacing?

To change spacing between baselines:
% to set one-and-half spacing
% put before \begin{document}
\renewcommand{\baselinestretch}{1.5}
More flexible way is provided by setspace package. It allows to change line spacing for parts of the text. To use the package, add before \begin{document}:
\usepackage{setspace}
\onehalfspacing % one-and-half spacing globally
% or \singlespacing % normal spacing
% or \doublespacing % double spacing
% or \setstretch{factor} % arbitrary spacing
Then after \begin{document} you can use onehalfspace and doublespace environments:
\begin{onehalfspace}
This text will be set with one-and-half line spacing.
\end{onehalfspace}
\begin{doublespace}
This text will be set with double line spacing.
\end{doublespace}

How to change margins?

The easiest way is to use geometry package, add before \begin{document}:
\usepackage[left=2cm,right=2cm,top=2cm,bottom=2cm,bindingoffset=0cm]{geometry}
To change layout and margins of the particular pages use chngpage package.

How to fit the text on one page?

First, try to \enlargethispage:
\enlargethispage{2\baselineskip} % add two more lines to this page
\pagebreak % finish the page
If you want to shrink the text, use the “starred” version \enlargethispage*{length}:
\enlargethispage*{\baselineskip}

How to rotate a page?

Use lscape package and the landscape environment it provides to rotate arbitrary pages. This may be useful to fit wide tables or figures:
% before \begin{document}
\usepackage{lscape}
% after \begin{document}
...
\begin{landscape}
...
This text will appear on a rotated, i.e. landscape, page.
...
\end{landscape}

Why figure numbering is wrong?

Check your figure environment. \label should always go after \caption. \caption first, \label second. The right template for the figure is:
\begin{figure}
... % insert your figure here
\caption{Caption text}
\label{fig:myfigure}
\end{figure}
See also figure numbers wrong in LaTeX.

How to make my own BibTeX style?

Fortunately, it is easy. Use makebst.tex program. In my system I can run it as
$ latex /usr/share/texmf-texlive/tex/latex/custom-bib/makebst.tex
Choose merlin as a template, choose any name for your style (e.g. mystyle), answer all the questions patiently. Finally, copy ile mystyle.bst to the directory with your article. Use it like this:
\bibliographystyle{mystyle}
\bibliography{bibfile}

How to enable/disable page numbering?

To disable page numbers:
\pagestyle{empty} % no page numbers
To enable page numbers:
\pagestyle{plain} % enable page numbers
To change it just for one page:
\thispagestyle{style}
To do more fancy things, use fancyhdr package.

How to make the PDF I produce with PDFLaTeX searchable?

Answer:
\usepackage{cmap}

How to strike-through or highlight the text?

To strike through words in LaTeX use package ulem:
% before \begin{document}:
\usepackage{ulem}
…
% in the text, after \begin{document}:
\sout{Wrong.} Right.
The result will be something like this:
Wrong. Right
To highlight words with different colour, use xcolor package:
% before \begin{document}
\usepackage{xcolor}
…
% after \begin{document}
I want some words to \textcolor{red}{stand out}.
Expect the result to be:
I want some words to be stand out.
If you want to change the background colour, \colorbox will help you:
% to make text appear on whitish blue (blue 50% – white 50%)
\colorbox{blue!50!white}{Text on whitish blue background}
And the result:
Text on whitish blue background

How to make notes on margins?

This is what \marginpar command does:
\marginpar{This text appears in the margin.}
Check also Todo notes in LaTeX by Henrik Skov Midtiby. He uses \marginpar to add side notes to the draft document. They look awesome (but don't work for me). I settled on a simplified version. Put before \begin{document}:
%%% todo notes, idea from
%%% http://midtiby.blogspot.com/2007/09/todo-notes-in-latex.html
%%%
\newcommand{\todo}[1]{
      \addcontentsline{tdo}{todo}{\protect{#1}}
      \colorbox{orange!90!black}{\textcolor{white}{\tt TODO}}
      \marginpar{\colorbox{orange!90!black}{\textcolor{white}{
      \parbox{4cm}{\small\tt\raggedright TODO:\\#1}
      }}}
}
\makeatletter
\newcommand\listoftodos{\clearpage\section*{Todo list} \@starttoc{tdo}}
\newcommand\l@todo[2] {\par\noindent {\Large $\Box$\;#1\dotfill\makebox[1.5em][r]{#2}\;}\par}
\makeatother
Then in the text add notes with:
\todo{rewrite this}
and finally add in the end:
\listoftodos