- gg: Go to the beggining of the document
- V: Enter visual mode
- G: Go to the end of the document
Easy, isn't it? Now you can press any other command to copy, delete or whatever you like.
Easy, isn't it? Now you can press any other command to copy, delete or whatever you like.
In LaTeX there's an easy way to handle source code. Though it was difficult to figure out, once in place this requires no maintenance at all - no need to manually syntax highlight your code nor update any files other than the source code itself. Even more, you can even change the source code language and it won't even matter in the presentation.
You'll need "pygmentyze" which is provided by the package python-pygments:
apt-cache search pygment
python-pygments - syntax highlighting package written in Python
sudo apt-get install python-pygments
Using it is really easy, check the manpage. Anyway, I'm too lazy to generate the syntax highlighted document by hand, let's add it to the makefile.
Let's beging by adding a "code" folder inside our project. We'll store there all the source code files, and having them in a separated folder will enable us to add a target in the makefile to automatically update the source code in the LaTeX file.
Remember the makefile from a couple of posts back? Let's add a target:
# Hack to make it work when foo.code => foo.code.tex
code/%.tex: code/%.* code/%.aux code/%.tex
@rm code/*.aux
$(MAKE) -C .
code/%.tex: code/%.*
pygmentize -f latex -O style='border=#000000,colorful,linenos=1' $< > $@
# Search each code file to format and include
CODE_FILES:=$(shell ls code/|egrep -v '.tex$ |.aux$ ' )
CODE_FILES_DEP:=$(addprefix code/, $(CODE_FILES))
CODE_FILES_TGT:=$(addsuffix .tex, $(basename $(CODE_FILES_DEP)))
main.pdf: $(shell ls *.tex) $(CODE_FILES_TGT)
pdflatex main.tex > /dev/null
OK, maybe a couple more than one. It may seem like a lot of makefile code but all it does is define a code directory and a target to run pygmentize on each source file found there. We'll have to add a dependency in the document's target so it'll be automatically generated with each build:
main.pdf: code_frames *.tex
pdflatex main.tex && pdflatex main.tex
and then, we'll need to clean up the new temp files:
code_clean:
@rm -f $(CODE_FILES_TGT) code/*.aux
Don't worry, there's a link to the full makefile.
It shouldn't have been too much work and we're done anyway. To include a source code file in your document now use the include command (like include{code/foobar.cpp) and re build. I'm attaching a complete example in a zip file, with my latest implementation of a bogosort algorithm (now 50% faster).
So far we've seen some of LaTeX advantages, and a few basic commands to get you started. Let's see a trick to be more proficient with it:
You should be able to write some simple documents now, some in LyX, some in LaTeX, but you'll soon start to notice that using a single text file to create a large document becomes cumbersome. Even more so if you need to split the work between several people in a team.
There's an easy way to keep a main file and then several, smaller, files in which you can work more comfortably:
include{file.tex}Beware, you can't use an include inside an include. Why? No idea, but there's a way around this:
input{file.tex}
Using includes has another advantage: you can have a quick preview while working with a chapter at a time. I usually keep the following structure within my projects:
% Header declarations
% Include packages
% Document preamble
% ...
%input{chapter1.tex}
%input{chapter2.tex}
input{chapter3.tex}
% input{chapter4.tex}
Just uncomment the chapter you're working with. In big documents this has a very noticeable effect, as compiling a large LaTeX file into an enormous pdf document (several MBs) may be quite slow.
Of course, I use "input" in my main file so I can use include in the chapters themselves. I won't usually need to include other documents inside the chapters, it'd get quite messy, but it's necessary to work with embedded documents, as we'll see in another post.
Did you knew that Vim can "Make your woman smile for a week"? Me neither: http://www.vim.org/trivia.php.

Remember I said that being a programmer would make you a lot more comfortable around LaTeX? The reason is quite simple, tex is just source code for a document. As with any source code in Linux (Windows too, but that is besides the point) you can use a Makefile to compile it and make your life easier.
I have already posted this Makefile in another entry but it's time to explain how it works.
all: main.pdf
main.pdf: code_frames *.tex
pdflatex main.tex && pdflatex main.tex
.PHONY: run clean edit
edit:
gvim -S vim.sess
run: main.pdf
evince main.pdf &
clean:
@# for each .tex file, remove the extension
@# and delete its generated files
@for PART in $(shell ls *.tex| sed 's:.tex::g'); do
echo "*.out *.nav *.aux *.toc *.log *.snm *.pdf *.vrb" | \
sed "s:*:$$PART:g" | xargs rm -f;
done
It's rather easy, let's check it target by target:
Short entry this time - next: using source code from within LaTeX.
Did you ever get to struggle against your text editor’s random format feature while trying to write a document? Open Office may be a great project, but when you want to focus on the content, it can be annoying to have your editor format or unformat your text, seemingly at random.
Well there are good news for those of us using Vim to create content and then Abiword to format it: LyX is a text editor that produces beautiful documents, without the need of being a designer, and yet manages to stay out of the way. From the tutorial and the homepage (www.lyx.org):
LyX is the first WYSIWYM (What You See Is What You Mean) document processor. The basic idea of LyX is that you do not need to handle style, or actually, you use a set of predefined styles and concentrate on your document content, This makes sure that your resulting document will be typographically correct and good looking visually. […] LyX uses Latex as its back end typesetting mechanism.
Upon start LyX looks more or less like any other graphical text editor in the universe:
[MISSING IMAGE]
Well, it’s logo may look nicer, but that’s about it. Anyway, the magic starts just as you start writing: you’ll notice most of the common format options seems missing, but you can define what you’re writing instead:
[MISSING IMAGE]
Note that we don’t tell it to center it or to make the font larger and bold. LyX takes care of all that automatically. Simply click on the format menu (below File, and it has the default value of “Standard”).
So instead of defining Times New Roman 12px bold centered, you say «Title». WYSIWYM, remember? In the homepage there is a «Graphical Tour» with all the basic functions, it’s quick and it’s great: www.lyx.org/LGT
LyX also provides a great support for math formulas (and all the weird symbols you can think off). Just click the button «Insert Equation» and a box to enter math symbols will appear. No more struggle to align the dividend and the divisor!
Of course, LyX provides the usual features such as tables, spell checking, footnotes and many more. The tutorial of the application is more than complete, and easy to follow.
LyX documents can be exported to a wide variety of formats, mainly because being based on Latex it takes advantage of the already existing conversion programs. Some of the possible export plugins installed by default are PS, PDF, DVI, Latex, HTML and Plain text, but custom ones may be defined.
What LyX isn’t for
Although LyX may be a valuable piece in anyone toolkit it’s worth noticing it isn’t exactly the Swiss army knife of the text editors. If you need to define a very customized layout or format, like slides for a presentation, this is the wrong tool for the job.
A good friend of mine (a programming junkie just like me) just started his blog, so this is kind of a welcome. Actually there's already a link somewhere in this page so this post is just an excuse to post this recommendation about writing articles for a blog:
Remember you didn't like books without pictures as a child? Well, Internet's mental age isn't a day over four years old, use pretty pictures whenever you can.
So, you're working on some macro magic incantation and you need a unique variable name in you C program? Though it may seem simple at first, using __LINE__ for a variable name, the ## operator (concatenation in the preprocesor) won't let you. There's a secret spell to do it anyway:
// Do magic! Creates a unique name using the line number
#define LINE_NAME( prefix ) JOIN( prefix, __LINE__ )
#define JOIN( symbol1, symbol2 ) _DO_JOIN( symbol1, symbol2 )
#define _DO_JOIN( symbol1, symbol2 ) symbol1##symbol2
Great, now you can keep obscuring your programs even more - have fun!
So, what's the most basic structural elements for any document you write? Paragraphs, of course. I ussualy use LyX for writing paragraph-based documents (i.e. most of them) and presentations don't have many, so perhaps this is the structural element I use the least. Oh, wait, there's no element, just a "nn". Point for LaTeX (take that, HTML).
You'll also need to put titles, sections, subtitles and all that stuff to give your document a nice structure. Well, that's easy too, just use
There are some other commands you'll use in any LaTeX document:
footnote{ Footnote text }
bigskip
begin{center}TEXTend{center}
begin{flushright}TEXTend{flushright}
You should now be able to write your first LaTeX document, starting from a template (always start with a template... it's easier). I'm attaching to this post an example document in LyX, LaTeX and its compiled pdf, in case you're wondering what does it look like in the end. For the next entry: some "advanced" tips and tricks, now let's see an example LaTeX document:
documentstyle[11pt]{article}
title{LaTeX Example}
author{Nicol'as Brailovsky}
setlength{topmargin}{-.5in}
setlength{oddsidemargin}{.125in}
setlength{textwidth}{6.25in}
begin{document}
maketitle
This is just a LaTeX kick off, with some basic commands.
section {Section one}
For example, this is a paragraph.
begin{quotation}
{ em Lorem ipsum dolor sit amet, consectetur adipiscing elit.}
Donec porta, enim eget tempus tempor, eros sem dapibus diam, vitae lacinia mauris metus id nulla.
end{quotation}
subsection{Subsection}
You can have subsections too ldots
subsection{Subsection'}
ldots as many as you want
subsection{Random stuff}
You can even create a shopping list in LaTeX, if you wish
begin{center}
begin{itemize}
item Beer
item Beer
begin{itemize}
item (Another brand of beer)
end{itemize}
item Pizza
item Beer
end{itemize}
end{center}
Or even better, use it to do the homework. \
Math is a breeze in LaTeX: $x_n = sqrt{a + b}$ can be typeset inline.
end{document}
You can download the compiled document from this link. May be it doesn't look too useful (I'd use LyX instead) but soon we'll start doing some cool stuff LyX can't handle. Keep tuned for the next LaTeX article.
Editing source code means you'll be doing a lot of indenting and reindenting. There's an easy way to indent or de-indent a block, using just "<" and ">".
Using ">" without anyother command will indent that line by itself; use visual mode (Shift + V) to select several lines and indent them in one keystroke. You could also press "v%>" while sitting at the beggining of a block to indent it. The same applies to "<".
My .vimrc file has been organically growing since I started using this editor, a couple of years ago, so I've decided to clean it up a little bit and post it here, just to be sure I won't be loosing it. It's got some tweaks to use Vim as an IDE, feel free to download it and change it or use it anyway you want.
colorscheme torte
set nocompatible
syntax on
set ruler
set number
set hls " Highlight search results
set showmatch " Show matching () {} []
set wildmode=list:longest,full " Use tab-completition
set mouse=a " Always use the mouse
" Set the working directory to the directory of the current file.
autocmd BufEnter * lcd %:p:h
" Allow movement to another buffer without saving the current one
set hidden
" *********** Text formatting *************
set nowrap
set beautify
set shiftwidth=3
set tabstop=3
filetype on
filetype plugin indent on
" *********** Search & replace *************
set ignorecase " case insensitive
set smartcase " case insensitive only if there is no uppercase
set incsearch " incremental seach
set gdefault " default to /g on replace
" Load matchit (% to bounce from do to end, etc.)
runtime! macros/matchit.vim
augroup myfiletypes
" Clear old autocmds in group
autocmd!
" autoindent with two spaces, always expand tabs
autocmd FileType ruby,eruby,yaml set ai sw=2 sts=2 et
augroup END
" display the current mode and partially-typed commands in the status line:
set showmode
set showcmd
set autoindent
set smartindent
" Show an error window (if there are errors)
cwindow
" *********** Mappings *************
" Ctrl-t: Write tabnew (wait for filename and )
map :tabnew
" Alt-R: Exec current file as script
map :!.%
" Ctrl-Alt-R
map :tabnew:make
" Spellcheck
map :!ispell -x %:e!
" Comment a line
map 0i//
map 0xxx
" Build for a LaTeX file (assumes correct path and makefile)
autocmd filetype tex map :w:make
" Automatic closing
kets
inoremap do{ do{}while();O
inoremap do{ do{}while();O
inoremap { {}O
Quick post this time - just a roundup of the "apt-get install new computer" series. This is a one liner I use in any new install I'm planning to work with, hope it's useful for you too.
sudo apt-get install lyx mozilla-thunderbird tilda mmv \
unclutter gnome-do moc ffmpeg shntool cuetools flac \
wavpack vlc deluge bittornado-gui build-essential \
gcc g++ valgrind latex-beamer ssh sshfs openssh-server \
vim vim-gtk sun-java5-bin sun-java5-jdk sun-java5-jre \
sun-java6-jdk antiword ifstat php5 php5-mysql php5-pgsql \
php5-cli php5-gd php5-curl ruby1.9 irb1.9 rails rake valgrind
New package: antiword - read office documents on console
Vim is the best editor for programmers and as such it has some neat "programmer commands", which make editing source code a lot easier. Take for example a block with code, or a function definition: how many times did you have to copy the code between parenthesis from one place to the other?
int foo(int bar, int baz)
You could do '0f(df)' to delete "int bar, int baz" but that won't do if the definition is more complicated than that. 'dib' is a better choice to delete the text.
(ACTION)i{b|B} applies ACTION to a block, for example, pressing diB in
void foo() _ {
/* Lots of source code */
}
(with the cursor placed at _) will delete lots of source code.
Check the previous post about this presentation.
Where I work we have been working a lot with unit testing and continuous integration. This is a presentation I worked with, about unit testing and mocking tools in C++ (only in spanish, sorry).
Where I work we have been working a lot with unit testing and continuous integration. This is a presentation I worked with, about unit testing and mocking in general (language agnostic but only in spanish, sorry). The examples are in pseudo-ruby, check the next post for a presentation on C++ tools for testing and mocking.
Remember last post? I mentioned it's possible to create several type of documents. Even if the structure will, most likely, be different for different applications, there's a common ground which can be used for most LaTeX documents. I'd go as far as saying ALL of them, but in LaTeX it's possible to override everything and anything. Any reasonable package won't do it, so let's see some LaTeX basics you can use for any project.
The first thing to understand about LaTeX, it's not WYSIWYG. If you use a plain text editor you won't see any kind of format at all (LyX provides some sort of preview though). This confuses some people who, used to Word-like applications, tend to think in the structure and desing before the contents; LaTeX is different in the way to structure documents, a way which is called What You See Is What You Mean, WYSIWYM, for short.
What does being WYSIWYM means? You just think about the content and structure and it gets formatted for you. Instead of saying you want "Centered, Arial 12px orange font with pink background" just say "this is a title". The format will be there, but not attached to the content - it'll be in a "stylesheet", the document's class (*). Of course, these styles can be changed too.
WYSIWYM has a downside too: you'll have to "compile" the mark-up text into a pdf (or any other format) using pdflatex, for example. It may be a shock to some but it's not difficult, just write "pdf2latex file.tex" and let it work its magic. You can use the makefile I'm attaching to this post too, if you feel comfortable with makefiles (The makefile has a few cool tricks you can use too. I'll explain all of it in another entry).
In GNU/Linux you can find lots of examples and templates in /usr/share/doc/texlive-doc/latex/ (no idea where may they be in Windows). Writing this article I found out lots of templates I didn't know about so go and take a look. Go on, I'll wait here. Done? Ok, let's move on.
In LaTeX there's a basic structure every document follows:
% Preamble
begin{document}
end{document}
What comes before the begin'ning is the preamble. There you can tell LaTeX:
This post is getting a little bit too large so I'll leave some basic commands for the next one; you should be able to start writing some basic documents anyway.
(*)
Enter line selection mode (Shift + V) and then select a block of text. Without moving the cursor any further type :s/SEARCH/REPLACE and the search string in the selected block will be replaced without altering any other part of the document.
| Today it's been 725328000 seconds of uptime since I was promoted from Release Candidate to V1.0. Still waiting for security updates though. | ![]() |