monochromatic

monochromatic blog: http://blog.z3bra.org
git clone git://z3bra.org/monochromatic
Log | Files | Refs

love-me-some-latex.txt (11206B)


      1 # [Love me some LaTeX](#)
      2 ## — 06 December, 2013
      3 
      4 
      5 I am now in my last year of engineering school, and this imply that
      6 I will have to write a report for the last 3 years I spent in my
      7 company, as an apprentice. As a fellow Linux user, I don't want to
      8 use that fucking text processor. I love my vim
      9 setup, and I want to use it to write that report.
     10 
     11 ### Introduction
     12 
     13 So i came to [LaTeX](http://www.latex-project.org/).  
     14 Latex (side note: I will not write "LaTeX" everytime in this post.
     15 I'm too lazy for that) is a kind of markup language that you can use
     16 to write scientific reports, or anything you want in fact.  
     17 
     18 It is intelligent in the sense that, you don't have to bother about
     19 the formatting on the document, latex will do that for you, and
     20 probably better than you. So you can just **focus on what your
     21 are typing**.
     22 
     23 Seems interresting huh? So basically, what does it look like? Here's
     24 a simple document (download/view it [HERE](http://pub.z3bra.org/monochromatic/misc/la.pdf))
     25 
     26     la.tex
     27 
     28     \documentclass[9pt]{report}     % Start of document
     29 
     30     \usepackage[utf8]{inputenc}     % Write text unsing UTF-8 encoding
     31     \usepackage{graphicx}           % Use graphics for diagrams and such
     32 
     33     \title{\LaTeX exemple}
     34     \author{z3bra, the cool guy}
     35     \date{\today}
     36 
     37     \begin{document}
     38 
     39     \maketitle
     40     \tableofcontents
     41     \listoftables
     42     \listoffigures
     43     \newpage
     44 
     45     \chapter{Introduction}
     46 
     47     \section{\LaTeX is fun}
     48     Bla bla, latex is cool, and not only in bed.
     49     See that explanatory figure to figure out why:
     50 
     51     \begin{figure}[h!]
     52     \centering
     53     \includegraphics[width=\textwidth]{figure}
     54     \caption{Figure caption}
     55     \label{fig:figure}
     56     \end{figure}
     57 
     58     that might not be the good figure. Anyway...
     59 
     60     \subsection{Love?}
     61     Do you love me some \LaTeX?
     62 
     63     \subsection{Llorem ipsum}
     64     My money's in that office, right? If she start giving me some bullshit about it
     65     ain't there, and we got to go someplace else and get it, I'm gonna shoot you in
     66     the head then and there. Then I'm gonna shoot that bitch in the kneecaps, find
     67     out where my goddamn money is. She gonna tell me too. Hey, look at me when I'm
     68     talking to you, motherfucker. You listen: we go in there, and that nigga Winston
     69     or anybody else is in there, you the first motherfucker to get shot. You
     70     understand?
     71 
     72     Now that there is the Tec-9, a crappy spray gun from South Miami. This gun is
     73     advertised as the most popular gun in American crime. Do you believe that shit?
     74     It actually says that in the little book that comes with it: the most popular
     75     gun in American crime. Like they're actually proud of that shit.
     76 
     77     Now that we know who you are, I know who I am. I'm not a mistake! It all makes
     78     sense! In a comic, you know how you can tell who the arch-villain's going to be?
     79     He's the exact opposite of the hero. And most times they're friends, like you
     80     and me! I should've known way back when... You know why, David? Because of the
     81     kids. They called me Mr Glass.
     82 
     83     \appendix
     84     \chapter{Appendix}
     85 
     86     \section{Oh! A table!}
     87     \begin{table}[h!]
     88     \begin{tabular}{ | l | l | p{5cm} | }
     89     % pretty that shit with !column -t -s \& -o \&
     90     \hline
     91     COLUMN & COLUMN 3 HEADER   & DESCRIPTION \\ \hline \hline
     92     column & name of something & Description of that, eeh \\ \hline
     93     column & name of something & Description of that, eeh \\ \hline
     94     column & name of something & Description of that, eeh \\ \hline
     95     column & name of something & Description of that, eeh \\ \hline
     96     column & name of something & Description of that, eeh \\ \hline
     97     column & name of something & Description of that, eeh \\ \hline \hline
     98     FOOTER & NAME OF FOOTER    & WHO CARES WHAT'S HERE?   \\ \hline
     99     \end{tabular}
    100     \caption{Exemple of a table}
    101     \label{tab:table}
    102     \end{table}
    103 
    104     \end{document}
    105     % vim: ft=tex tw=80 cc=81 nowrap fdm=marker :
    106 
    107 
    108 Wooow ! That's so many tags for such a simple thing!  
    109 In fact, once you get used to those tags, they are no more a
    110 problem. Also, The more text you have, the less you'll see the tags.
    111 
    112 
    113 So, now you know HOW latex looks like. But you don't know HOW to
    114 write it, and what to do with that file.  
    115 Don't worry, that ain't difficult. 
    116 
    117 
    118 ### Writing latex
    119 
    120 Latex files basically looks like any source file. First come the
    121 definitions, and then the source (document) itself.  
    122 The definitions are importants because that will tell latex HOW to
    123 create the document.
    124 
    125 Latex tags are only a \ followed by a command. If that keyword needs
    126 an argument, that argument is given within {}. If that command needs
    127 an option, that option is given within []. Seems hard ? It is not.
    128 Take a look at this:
    129 
    130     \command[option]{argument}
    131     % comments are from % 'til EOL
    132 
    133 Do you get it now ? Then, move on.
    134 
    135 The simplest header would be:
    136 
    137     \documentclass{report}
    138 
    139 
    140 This will tell to latex that you want to write a report. Latex knows
    141 what a report is, so it will apply a set of rules (margin, font
    142 size, numerotation of chapters/section, and so on). So you will ONLY
    143 have to rewrite the rules you don't like, or that are missing.
    144 I'll not cover that in this post, because there are too many way
    145 to do so, and you are tall enough to find infos by yourself 
    146 ;). Latex has a huge wiki, which is
    147 great. Use it!
    148 
    149 
    150 Now, the body. Here to, I will give you the most basic body (eg, not
    151 covering figures, or table. The wiki is better than me to that).
    152 So, the most simple body is the following:
    153 
    154     \begin{document}
    155 
    156     \chapter{First chapter}
    157 
    158     \section{\LaTeX is fun}
    159     My money's in that office, right? If she start giving me some bullshit about it
    160     ain't there, and we got to go someplace else and get it, I'm gonna shoot you in
    161     the head then and there. Then I'm gonna shoot that bitch in the kneecaps, find
    162     out where my goddamn money is. She gonna tell me too. Hey, look at me when I'm
    163     talking to you, motherfucker. You listen: we go in there, and that nigga Winston
    164     or anybody else is in there, you the first motherfucker to get shot. You
    165     understand?
    166 
    167     \section{Middle section}
    168     Now that there is the Tec-9, a crappy spray gun from South Miami. This gun is
    169     advertised as the most popular gun in American crime. Do you believe that shit?
    170     It actually says that in the little book that comes with it: the most popular
    171     gun in American crime. Like they're actually proud of that shit.
    172 
    173 
    174     \chapter{Last chapter}
    175 
    176     \section{Last section}
    177     Now that we know who you are, I know who I am. I'm not a mistake! It all makes
    178     sense! In a comic, you know how you can tell who the arch-villain's going to be?
    179     He's the exact opposite of the hero. And most times they're friends, like you
    180     and me! I should've known way back when... You know why, David? Because of the
    181     kids. They called me Mr Glass.
    182 
    183     \end{document}
    184 
    185 
    186 You're still there ? Fine. Now, what should we do with that file ?
    187 
    188 
    189 ### Compilation
    190 
    191 Yeah, you read it right. You compile a Latex file, because it has to
    192 generate many other files in order to understand your whole
    193 document.  
    194 It will have to count yur chapters, sections, subsection, and so on.
    195 Then, create a output file which will be formatted, and correctly
    196 numeroted, linked to your figures, tables, etc..
    197 
    198 
    199 So, how does it works ? Taht's easy, just call `latex` on your file to have it
    200 compiled.
    201 
    202     latex file.tex
    203 
    204 This command will create a **dvi** file. If you want
    205 another format, you you be able to use any other tool to convert it
    206 to the fomat you want:
    207 
    208     dvips  file.dvi
    209     dvipdf file.dvi
    210 
    211 This will create two files: `file.ps`, and `file.pdf` (U DONT SAY?!), that you
    212 will be able to view in your prefered PDF viewer. (I suggest
    213 [zathura](http://pwmt.org/projects/zathura/), which is able to reload
    214 automatically your file if the timestamp have changed)
    215 
    216 
    217 I here someone in the audience whispering:
    218 <q>But, That's a pain !</q>. You are not wrong ! That's why my
    219 favorite tool comes to the rescue: `make`.
    220 Yes ! Each time you hear or read the word _compilation_, you
    221 should instantly think _Makefile_. That's what I did, and it
    222 has saved me a lot of time!  
    223 I could leave it to you as an exercise, but I know that some of you
    224 are just lazy. So here is the makefile I came with:
    225 
    226     # File name
    227     BASE    = la
    228 
    229     # How to view output files
    230     VIEWER  = zathura
    231 
    232     # Files used for diagrams (umlet file)
    233     uxffiles=$(wildcard fig/*.uxf)
    234 
    235     # Those files, with the correct extension for LaTeX
    236     figures=$(patsubst %.uxf,%.eps,${uxffiles})
    237 
    238     .SUFFIXES:
    239     .SUFFIXES:.ps .pdf .dvi .tex .uxf .eps .toc .lof .lot
    240 
    241     .PHONY: default all dvi ps pdf eps view view-ps view-pdf clean mrproper
    242 
    243     # What should 'make' run by default ?
    244     default: ps
    245 
    246     # build everything
    247     all: ps pdf view-ps
    248 
    249     # The output files
    250     dvi: ${BASE}.dvi
    251     ps:  ${BASE}.ps
    252     pdf: ${BASE}.pdf
    253     eps: ${figures}
    254 
    255     # Default view
    256     view: view-ps
    257 
    258     # --- DVI ---------------------------------------
    259     ${BASE}.dvi: ${figures} ${BASE}.toc ${BASE}.tex
    260     	@echo &quot;TEX  ${BASE}.tex&quot;
    261     	@latex ${BASE}.tex &gt;/dev/null
    262 
    263     # --- TOC ---------------------------------------
    264     ${BASE}.toc: ${figures} ${BASE}.tex
    265     	@echo &quot;TOC  ${BASE}.tex&quot;
    266     	@latex -draftmode ${BASE}.tex &gt;/dev/null
    267 
    268     # --- EPS ---------------------------------
    269     .uxf.eps:
    270     	@echo &quot;EPS  $&lt;&quot;
    271     	@umlet -action=convert -format=eps -filename=$&lt; &gt;/dev/null
    272 
    273     # --- PS ----------------------------------------
    274     .dvi.ps:
    275     	@echo &quot;PS   $&lt;&quot;
    276     	@dvips -q $&lt;
    277 
    278     # --- PDF ---------------------------------------
    279     .dvi.pdf:
    280     	@echo &quot;PDF  $&lt;&quot;
    281     	@dvipdf -q $&lt;
    282 
    283     # --- OTHER -------------------------------------
    284     view-%: ${BASE}.%
    285     	${VIEWER} $&lt;
    286 
    287     clean:
    288     	rm -f *.aux
    289     	rm -f *.log
    290     	rm -f *.dvi
    291     	rm -f *.lof
    292     	rm -f *.lot
    293     	rm -f *.toc
    294 
    295     mrproper: clean
    296     	rm -f fig/*.eps
    297     	rm -f *.pdf
    298     	rm -f *.ps
    299 
    300 Easy to use!
    301 
    302     make [dvi|ps|pdf|eps]   # build either the dvi/ps/pdf files, or the eps files
    303     make [view-ps|view-pdf] # calls ${VIEWER} on either the ps or pdf file
    304 
    305 Maybe... To get the "eps" part, see the [tips section](#tips)
    306 
    307 ### Enjoy !
    308 
    309 There you go, you are now ready to write your own latex documents,
    310 and browser the web to find help about what is not explained in
    311 this post (there is **A LOT** things about latex..)
    312 
    313 <h3 id='tips'>Tips</h3>
    314 
    315 Before leaving you, here are just a few tips, from my experience
    316 with latex (particularly images).
    317 
    318 * Read other documents, there are goldmine out there, and that
    319   will help you a lot
    320 * Write the skeleton of your file BEFORE the text itself, so you
    321   can focus on what you type.
    322 * DVI format only support EPS files to render figures, now you're
    323   warned..
    324 * [UMLet](http://umlet.com/) is a great tool to create
    325   diagrams and such. It also include a cli routine to export files
    326   to format like pdf/jpg/eps (see the Makefile above)
    327 * Latex can also write letter! That's cool isn't it?
    328 
    329 <!-- vim: set ft=markdown ts=4 et: -->