Domů » Informatika » Programovací jazyk » Základy jazyka LaTeX » Jazyk LaTeX – Sazba kódu a pseudokódu

Jazyk LaTeX – Sazba kódu a pseudokódu

Makro Listings (kód)

Pro sazbu programového kódu s možností zvýraznění syntaxe lze použít balík listings.

kód v jazyce LATEX - Zobrazit

  1. \usepackage{listings}

Pro lepší vzhled je lepší jej nastavit zhruba takto:

kód v jazyce LATEX - Zobrazit

  1. \usepackage[usenames,dvipsnames]{color}
  2. \usepackage{listings}
  3. \lstset{
  4.   columns=fullflexible,
  5.   language=Java,
  6.   numbers=left,
  7.   numbersep=5pt,
  8.   basicstyle=\small,
  9.   numberstyle=\footnotesize\color{Gray},
  10.   commentstyle=\it\footnotesize\color{Gray}
  11. }
Příklady
Zdrojový kód

kód v jazyce LATEX - Zobrazit

  1. \begin{minipage}{\paperwidth}
  2. \begin{lstlisting}
  3. // zdrojový kód
  4. \end{lstlisting}
  5. \end{minipage}
Kód ze souboru

kód v jazyce LATEX - Zobrazit

  1. \lstinputlisting{source.c}
Popisek

Nad zdrojový kód stačí uvést následující řádek:

kód v jazyce LATEX - Zobrazit

  1. \lstset{
  2.   language=C,
  3.   caption={Popisek},
  4.   label=oznaceni
  5. }

Makro Algorithmic (pseudokód)

Pro sazbu pseudokódu lze použít například balík algorithmic.

kód v jazyce LATEX - Zobrazit

  1. \usepackage{algorithmic}
Příklady
Genetický algoritmus

kód v jazyce LATEX - Zobrazit

  1. \begin{figure}
  2. \centering
  3. \begin{algorithmic}[1]
  4. \STATE{create an initial population $ P_0 $ (usually random)}
  5. \STATE{evaluate the fitness of each invidivual in $ P_0 $}
  6. \FOR{$ g $ in range 1 .. $ g_{max} $}
  7. \STATE{create a new empty population $ P_g $}
  8. \STATE{take individuals from $ P_{g - 1} $ using the selection operator and copy them into the new population $ P_g $ either directly or using crossover and mutation operators}
  9. \STATE{evaluate the fitness of each individual in $ P_g $}
  10. \STATE{replace the old population $ P_{g - 1} $ by the new population $ P_g $}
  11. \ENDFOR
  12. \RETURN{best-ranked individual from $ P_g $}
  13. \end{algorithmic}
  14. \caption{Genetic algorithm pseudocode (generational model)}
  15. \label{fig:ga_pseudocode_dynamic}
  16. \end{figure}
Turnajový výběr

kód v jazyce LATEX - Zobrazit

  1. \begin{figure}
  2. \centering
  3. \begin{algorithmic}[1]
  4. \STATE{take $ N $ random individuals from the population}
  5. \IF{random real number $ \in \langle 0, 1) < 0.95 $}
  6. \RETURN{individual with the best fitness}
  7. \ELSE
  8. \RETURN{random individual}
  9. \ENDIF
  10. \end{algorithmic}
  11. \caption{The tournament selection algorithm pseudocode}
  12. \label{fig:tournament}
  13. \end{figure}

Reference