• Algorithmique et Programmation en LaTeX

    Algoritmique et Programmation en LaTeX

    Cette année, j'ai des cours d'algorithmique et de programmation, du coup je fais mes rapports en LaTeX. Un de mes collègues m'a passé de quoi intégrer du code et des algorithmes dans vos rapports, et ce proprement et joliment.

    Listings de Programmation

    Pour afficher des bouts de code, rien de bien compliqué. On commence par charger l'environnement listing.

    \usepackage{listings}
    [code=Latex]
    \usepackage{listings}[/code]

    Ensuite, on le configure un peu.

    \lstset{
    language=C,
    keywordstyle=bfseriesttfamilycolor[rgb]{0,0,1
    },
    identifierstyle=ttfamily,
    commentstyle=color[rgb]{0.133,0.545,0.133},
    stringstyle=ttfamilycolor[rgb]{0.627,0.126,0.941},
    showstringspaces=false,
    basicstyle=footnotesize,
    numberstyle=footnotesize,
    numbers=left,
    stepnumber=1,
    numbersep=10pt,
    tabsize=2,
    breaklines=true,
    breakatwhitespace=false,
    aboveskip={1.5baselineskip},
    columns=fixed,
    upquote=true,
    extendedchars=true,
    }
    [code=Latex]\lstset{
    language=C,
    keywordstyle=bfseriesttfamilycolor[rgb]{0,0,1},
    identifierstyle=ttfamily,
    commentstyle=color[rgb]{0.133,0.545,0.133},
    stringstyle=ttfamilycolor[rgb]{0.627,0.126,0.941},
    showstringspaces=false,
    basicstyle=footnotesize,
    numberstyle=footnotesize,
    numbers=left,
    stepnumber=1,
    numbersep=10pt,
    tabsize=2,
    breaklines=true,
    breakatwhitespace=false,
    aboveskip={1.5baselineskip},
    columns=fixed,
    upquote=true,
    extendedchars=true,
    }
    [/code]

    Rien de bien sorcier dans la config, en bidouillant un peu, on peut obtenir ce que l'on veut (langage, espacements, etc...) Il suffit ensuite d'encapsuler le bout de code qu'on veut afficher dans un environnement lstlisting

    \begin{lstlisting}
     #include <stdio.h>
     int main(){
         printf("Hello World");
         return 0;
     }
     \end{lstlisting}
    [code=C]\begin{lstlisting}
    #include <stdio.h>
    int main(){
        printf("Hello World");
        return 0;
    }
    \end{lstlisting}[/code]

    Algoritmique

    Pour afficher des algorithmes dans vos documents LaTeX, rien de bien compliqué non plus, sauf qu'on va franciser un peu les packages dont on se sert (personnellement je trouve ça affreux, mais on est obligés de faire comme ça en cours :/). On commence par charger les packages.

    \usepackage{algorithm,algorithmic};
    [code=Latex]\usepackage{algorithm,algorithmic};[/code]

    On redéfinit ensuiteles commandes des packages algorithmic et algorithm.

    % Francisation des algorithmes
    \renewcommand{algorithmicrequire} {textbf{textsc{Entrées:}}}
    \renewcommand{algorithmicensure}  {textbf{textsc{Sorties:}}}
    \renewcommand{algorithmicwhile}   {textbf{tant que}}
    \renewcommand{algorithmicdo}      {textbf{faire}}
    \renewcommand{algorithmicendwhile}{textbf{fin tant que}}
    \renewcommand{algorithmicend}     {textbf{fin}}
    \renewcommand{algorithmicif}      {textbf{si}}
    \renewcommand{algorithmicendif}   {textbf{fin si}}
    \renewcommand{algorithmicelse}    {textbf{sinon}}
    \renewcommand{algorithmicthen}    {textbf{alors}}
    \renewcommand{algorithmicfor}     {textbf{pour}}
    \renewcommand{algorithmicforall}  {textbf{pour tout}}
    \renewcommand{algorithmicdo}      {textbf{faire}}
    \renewcommand{algorithmicendfor}  {textbf{fin pour}}
    \renewcommand{algorithmicloop}    {textbf{boucler}}
    \renewcommand{algorithmicendloop} {textbf{fin boucle}}
    \renewcommand{algorithmicrepeat}  {textbf{répéter}}
    \renewcommand{algorithmicuntil}   {textbf{jusqu'à}}
    \renewcommand{algorithmiccomment} {STATE //}
    \newcommand{BEGIN}{STATE fbox{Début}}
    \newcommand{END}{STATE fbox{Fin}}
    \floatname{algorithm}{Algorithme}
    [code=Latex]% Francisation des algorithmes
    \renewcommand{algorithmicrequire} {textbf{textsc{Entrées:}}}
    \renewcommand{algorithmicensure}  {textbf{textsc{Sorties:}}}
    \renewcommand{algorithmicwhile}   {textbf{tant que}}
    \renewcommand{algorithmicdo}      {textbf{faire}}
    \renewcommand{algorithmicendwhile}{textbf{fin tant que}}
    \renewcommand{algorithmicend}     {textbf{fin}}
    \renewcommand{algorithmicif}      {textbf{si}}
    \renewcommand{algorithmicendif}   {textbf{fin si}}
    \renewcommand{algorithmicelse}    {textbf{sinon}}
    \renewcommand{algorithmicthen}    {textbf{alors}}
    \renewcommand{algorithmicfor}     {textbf{pour}}
    \renewcommand{algorithmicforall}  {textbf{pour tout}}
    \renewcommand{algorithmicdo}      {textbf{faire}}
    \renewcommand{algorithmicendfor}  {textbf{fin pour}}
    \renewcommand{algorithmicloop}    {textbf{boucler}}
    \renewcommand{algorithmicendloop} {textbf{fin boucle}}
    \renewcommand{algorithmicrepeat}  {textbf{répéter}}
    \renewcommand{algorithmicuntil}   {textbf{jusqu'à}}
    \renewcommand{algorithmiccomment} {STATE //}
    \newcommand{BEGIN}{STATE fbox{Début}}
    \newcommand{END}{STATE fbox{Fin}}
    \floatname{algorithm}{Algorithme}[/code]

    Pour intégrer un algo dans son document LaTeX, il ne reste plus qu'à faire comme ceci :

    \begin{algorithm}
      \caption{Algo de test}
      \begin{algorithmic}
        \require entiers a, b
        \ensure entier d
        \begin
          \state d $leftarrow (a+b)$
        \end
      \end{algorithmic}
    \end{algorithm}
    [code=Latex]\begin{algorithm}
      \caption{Algo de test}
      \begin{algorithmic}
        \require entiers a, b
        \ensure entier d
        \begin
          \state d $leftarrow (a+b)$
        \end
      \end{algorithmic}
    \end{algorithm}[/code]

    Normalement, vous devriez avoir tout ce qu'il faut pour impressionn^Wfaire plaisir à votre prof d'Algo - Programmation préféré(e). Bon courage et bonne programmation !

    Merci à Julien pour m'avoir passé ces bouts de code !

    « Victoire !Un peu de Conky »

    Tags Tags : , ,
  • Commentaires

    1
    Chebitchov
    Dimanche 5 Avril 2009 à 22:00
    C'est sympa comme petite introduction.

    Par contre, pourquoi tous les "\" ont sauté ? Moi qui ait l'habitude de faire du copier-coller sauvage, je me suis retrouvé fort marri...
    2
    Dimanche 5 Avril 2009 à 23:13
    Un petit problème de parsing, je vais essayer de régler ça
    3
    Samedi 25 Avril 2009 à 23:21
    Ca donne envie de regarder de plus près LateX :)
    4
    Xklr65
    Mardi 30 Mars 2010 à 09:08

     Il faut savoir qu'il existe une commande qui permet de charger un fichier complet directement dans son .tex


    Exemple:


    \lstinputlisting{fichier.c}


    C'est tres pratique, car on n'a pas a recopier tout son code dans le .tex (ce qui permet de garder des codes a jour)

    5
    Mardi 30 Mars 2010 à 10:57
    Good to know !
    Merci ;-)
    Suivre le flux RSS des commentaires de cet article


    Ajouter un commentaire

    Nom / Pseudo :

    E-mail (facultatif) :

    Site Web (facultatif) :

    Commentaire :