Quote of the day

If you can’t see any error messages it must be because you didn’t start it. Or it has already core-dumped.


LaTeX basics

LaTeX basics

Remember last post? I mentioned its 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.

typesetting

LaTeX documents

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).

Seeing some examples

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.

Preamble

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:

  • Which packages are you going to use – there are lots of packages, some examples include source code highlighting, including images, absolute positioning of figures and much more. Include them with the usepackage command
  • The style – there are stylesheets you can use, just as you do with HTML, and the preamble is the place to define them. How to do it is package-dependent, Beamer, for example, uses a usetheme tag.
  • Global declarations – You may declare an image which you’ll use throughout the entire document, you’re company’s logo for example
  • The author(s) – OK, this is a global declaration too, but there is certain meta-data which is declared in the preamble that’s quite handy and author is one of them.
  • The title – same as before, the title is very important in your document!
  • Lots more – there are package dependent declarations so there’s no way to create a comprehensive list, just remember it’s a place to put global settings

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.

(*) <Rant mode> Yeah, something like content and style separation but only a couple of decades before it was “invented” for the WWW.</Rant mode>


Vim Tip: Partial Search & Replace

vim_editor So, you have a paragraph, or any kind of text, and need to replace a substring without altering other paragraphs. You could write :set number and then :A,Bs/what to search/what to replace/g, being A and B the start and end of the paragraph, or you could just use visual mode.

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.


self.uptime = 725328000

Today it’s been 725328000 seconds of uptime since I was promoted from Release Candidate to V1.0. Still waiting for security updates though. new-super-mario-bros

Vim: Success

The other day I convinced someone Vim is THE true editor.

success_vim

Success
Disclaimer: I love that image and would have found other excuse to use it


A case for LaTeX

lion So, after struggling with you word processor to get an acceptable format you decide to hunt the repos (you’re using Linux, right?) for a better alternative. We already know why would you choose LyX but why would you choose LaTeX?

Let’s begin by listing some of its advantages

  • Wide range of applications – LaTeX is a widely used format, with lots of years, users and fields of application. It’s not used only to create papers or just presentations, it can be used in a wide range of applications.
  • There are “packages” prepared for each type of document – related to the previous point, you can find packages to create a CV, a presentation, posters, etc.
  • No need to format every single element – everything comes with a reasonable default format. You may fight a little bit whenever you need to change the default format (though you’ll find it less necessary each day) but 90 percent of the time “it just works”. With regular word processors it’s usually the other way around (it is for me!)
  • Works from a console – OK, this may be an advantage or a disadvantage, according to whom you ask. It sure is an advantage for us, the crazy console people, who don’t like to drop the CLI just for a couple of slides.
  • Great templates – no need to work in the design of the documents, that’s already taken care of.
  • Plain text format – easy to integrate with any source code management system.
  • Easy to integrate with external media (images, for example) – did you just change that image? No problem, just “recompile” to get it on your document.
  • The best math system

It has some downsides too

  • Steep learning curve
  • Requires some familiarity with the console
  • There’s a lot of markup to learn – this can lead to a low signal to noise ratio for some packages like Beamer and the need to RTFM more often than usual.
  • Creating a LaTeX document requires certain syntax – it’s not quite like programming, but programmers will be much more comfortable around it
  • Weird error messages – the LaTeX compiler will create an enormous amount of output and error messages may not allways be easy to comprehend.
  • It’s a one way ticket – you may be Word – impaired for the rest of your life
  • Who should use LaTeX?

    As previously stated, programmers will be more comfortable around plain LaTeX but that’s not the only pre requisite – you should be somewhat familiar with the console in Linux (no idea about LaTeX on Windows) and have some patience to read a couple of manuals. Once you have mastered the basics it’s much easier, so don’t get discouraged.

    If you feel you don’t fit the profile perhaps LyX is a better alternative than plain LaTeX; it’s very easy to use and quite user friendly. It won’t be that great for presentations and other, more “advanced”, documents though.

    What kind of work can you do with LaTeX?

    That’s an easy one, check My Articles section for some examples. You can create any kind of document but it’s better suited for those in which you care most about the content and don’t want to get bogged down with perky design details.

    Editors

    Last (for this entry) but not least, what editor can you use to create LaTeX documents? Well, LaTeX is basically plain text so any editor will do. There are some LaTeX-specific editors, I like ViM myself. There’ll be a complete post dedicated to LaTeX + Vim, coming soon.

    This article will be the first in a series of LaTeX survival guide so stay tuned.


    apt-get install new computer III

    Check the other two parts of this series:

    tux-in-tux

    I recently updated to Ubuntu 9.04 and had to reinstall my work computer. Since the stuff I use for work (mostly c++ programming) was left out in the previous two posts I’ll post it here:

    • Install build tools
    sudo apt-get install g++ gdb build-essential g++-4.2 omniorb4 omniidl4-python omniidl4 libxerces-c2-dev
    • Can’t live without VIM
    sudo apt-get install vim gvim vim-common vim-doc vim-full vim-gnome vim-gtk
    • Some additional tools
    sudo apt-get install ddd devhelp doxygen doxygen-gui exuberant-ctags ctags  subversion
    • Install bandwith monitor
    sudo apt-get install ifstat
    • Don’t you hate when you need to read an Office document while working on the console? Me too, try antiword:
    sudo apt-get install antiword

    Done, a couple of apt-get’s and you are ready to build your c++ projects! (Ok, actually only the first one is needed, the others are nice-to-have programs).


    Everything is a file A.K.A. Battery state on Linux

    I present to you the latest release from Wheel Reinventions Inc. – Already Invented dept. – a script to check the remaining battery time from Linux console.

    echo -n "Hours remaining: " 
    &amp;&amp; echo "$(cat /proc/acpi/battery/BAT0/state 
          | grep 'remaining capacity' 
          | awk '{print $3}') /  
       $(cat /proc/acpi/battery/BAT0/state 
          | grep 'present rate' 
          | awk '{print $3}')" 
       | bc -l
    car_battery

    Yes, I was bored, so? I’m planning to release a new bogosort implementation after this one, what do you think?

    Jokes aside, this nifty script shows how flexible can be the model “everything is a file” which Unix-y systems implement. With a couple of pipes (and an almost magical incantation for those uninitiated in the console cult) you can do lots of stuff with very little work

    This concept could be associated with the way polymorfism works in a programming language, but that’s an entry for another day.


    LaTeX!

    800px-latex_logosvg Some time ago I wrote a review about LyX for DPOTD (recommended link) and now I think it’s time for an update: what can you do when LyX is not enough?

    I’ll quote myself; the original article ends (almost) like this:

    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.

    After having prepared several presentations with LyX (even my CV!) I’d like to write some more on this last part. Even if I thought that my original comment was mistaken I write once again the same: when you have to create a presentation LyX is not the tool for the job, LaTeX is.

    As I explained in that article, LyX is a front-end for LaTeX but at the same time it’s only a small part of LaTeX. Doing several presentations in LyX (using Beamer, a package I’ll talk about in another post) I noticed that most of my documents where “ERT”, evil red text, which is nothing else than plain TeX code: that means, each time more, I was using LaTeX functionality not available from LyX. This is the reason I’ve decided to write a how-to LaTeX, a small survival guide for those want to start using with the tips and tricks I’ve learned in this time.

    The target for these posts series will be, mainly, programmers or other advanced users, without fear of working a little bit in the console, markup languages and maybe a makefile.

    Soon the first part 🙂


    Vim Tip: Visual Mode

    vim_editor There’s normal mode, command mode and a third, less known to the begginers, visual mode. What’s this mode about?

    Using Vim on visual mode can make some rather complex tasks and regex operations go away with a couple of keystrokes and it’s as easy as selecting the text you want with the cursor.

    Move your cursor over the first part of the text you’re trying to select and press Ctrl + V to set Vim to visual mode, then move the cursor to the last part of the text you’re trying to select (it works in blocks, not character by character nor line by line… try it, it’s easier than it may seem).

    What can you do after selecting text this way? Pretty much anything else you’d do with normal text, like cut (x) and paste (p). This is specially useful when working with formated text (i.e. programming).

    Visual mode can also work with lines (use Shift + V insted. Notice the uppercase V) and with other modes; check the manual for more information.