Showing posts with label package. Show all posts
Showing posts with label package. Show all posts

Wednesday, May 27, 2009

Insert source code with syntax highlighting in LaTeX

To insert source code with syntax highlighting, one possibility is to use the Listings package.

The Listings package supports a huge number of languages: ABAP, IDL, Plasm, ACSL, inform, POV, Ada, Java, Prolog, Algol, JVMIS, Promela, Ant, ksh, Python, Assembler, Lisp, R, Awk, Logo, Reduce, bash, make, Rexx, Basic, Mathematica1, RSL, C, Matlab, Ruby, C++, Mercury, S, Caml, MetaPost, SAS, Clean, Miranda, Scilab, Cobol, Mizar, sh, Comal, ML, SHELXL, csh, Modula-2, Simula, Delphi, MuPAD, SQL, Eiffel, NASTRAN, tcl, Elan, Oberon-2, TeX, erlang, OCL, VBScript, Euphoria, Octave, Verilog, Fortran, Oz, VHDL, GCL, Pascal, VRML, Gnuplot, Perl, XML, Haskell, PHP, XSLT, HTML, and PL/I.



To use the Listings package, first define the package in the preamble.
\usepackage{listings}
Second, define the language to use, anywhere in the document.
\lstset{language=Python}
Finally, insert the source code.

  1. write code within your document
    \begin{lstlisting}
    put your code here
    \end{lstlisting}

  2. import the code from other file
    \lstinputlisting{source_filename.py}

For more information, read

  1. PDF by Carsten Heinz

  2. Listings package on CTAN

Monday, May 18, 2009

How to create a matrix in LaTeX

We can create a matrix in LaTeX using the array environment, or simplematrix, matrix, pmatrix, bmatrix, vmatrix, and Vmatrix environments via amsmath package. This article provides some examples on how to create a matrix in LaTeX.

Creating a matrix with array

Here are some examples.
  1. unbracketed matrix

    \[
    M =
    \begin{array}{cc}
    x & y \\
    z & w
    \end{array}
    \]
  2. matrix surrounded by square brackets

    \[
    M =
    \left[ {\begin{array}{cc}
    x & y \\
    z & w
    \end{array} } \right]
    \]
  3. matrix surrounded by parentheses

    \[
    M =
    \left( {\begin{array}{cc}
    x & y \\
    z & w
    \end{array} } \right)
    \]
  4. matrix surrounded by single vertical lines

    \[
    M =
    \left| {\begin{array}{cc}
    x & y \\
    z & w
    \end{array} } \right|
    \]

Using amsmath package

Call \usepackage{amsmath} in the preamble, after documentclass{}.

The amsmath package environment for matrix:
  1. smallmatrix: inline matrix


    $M = \begin{smallmatrix}
    x & y \\
    z & w
    \end{smallmatrix}$

    $M = \left( \begin{smallmatrix}
    x & y \\
    z & w
    \end{smallmatrix}\right)$
  2. matrix: unbracketed matrix
    $M = \begin{matrix}
    x & y \\
    z & w
    \end{matrix}$

  3. pmatrix: matrix surrounded by parentheses
    $M = \begin{pmatrix}
    x & y \\
    z & w
    \end{pmatrix}$
  4. bmatrix: matrix surrounded by square brackets
    $M = \begin{bmatrix}
    x & y \\
    z & w
    \end{bmatrix}$
  5. vmatrix: matrix surrounded by single vertical lines
    $M = \begin{vmatrix}
    x & y \\
    z & w
    \end{vmatrix}$
  6. Vmatrix: matrix surrounded by double vertical lines

    $M = \begin{Vmatrix}
    x & y \\
    z & w
    \end{Vmatrix}$