| 1 |
\chapter{Including Code Listings}\label{app:codelistings} |
| 2 |
This appendix provides guidelines for including code listings in your thesis. |
| 3 |
Code listings are often used to demonstrate algorithms, data processing scripts, or other relevant programming content. |
| 4 |
Proper formatting ensures that code is both readable and aesthetically pleasing. |
| 5 |
|
| 6 |
\section{Using the \pkg{listings} Package} |
| 7 |
The \pkg{listings} package is a powerful tool for displaying code in LaTeX. |
| 8 |
It supports syntax highlighting for a wide variety of programming languages and offers many customization options. |
| 9 |
|
| 10 |
\subsection{Basic Usage} |
| 11 |
|
| 12 |
To include a simple code listing, you can use the following command: |
| 13 |
|
| 14 |
\noindent\lstinline|\begin{lstlisting}[language=Python]|\\[-0.7em] |
| 15 |
\lstinline| # Your code here|\\[-0.7em] |
| 16 |
\lstinline| print("Hello, world!")|\\[-0.7em] |
| 17 |
\lstinline|\end{lstlisting}| |
| 18 |
|
| 19 |
The \opt{language} option specifies the programming language, which enables syntax highlighting. |
| 20 |
Replace \opt{Python} with the appropriate language for your code. |
| 21 |
|
| 22 |
% INSERT EXAMPLE: Consider adding a few more examples showing code listings in different languages (e.g., C++, Java, MATLAB). |
| 23 |
|
| 24 |
\subsection{Customizing Listings} |
| 25 |
|
| 26 |
The \pkg{listings} package allows for extensive customization. |
| 27 |
You can adjust the appearance of your code by setting options such as \opt{frame}, \opt{backgroundcolor}, \opt{keywordstyle}, and more. |
| 28 |
|
| 29 |
Here is an example of how to customize your code listing: |
| 30 |
|
| 31 |
\begin{lstlisting}[style=LaTeXStyle] |
| 32 |
\lstset{ |
| 33 |
language=Python, |
| 34 |
frame=single, |
| 35 |
backgroundcolor=\color{gray!10}, |
| 36 |
keywordstyle=\color{blue}\bfseries, |
| 37 |
commentstyle=\color{green}, |
| 38 |
stringstyle=\color{red}, |
| 39 |
basicstyle=\ttfamily, |
| 40 |
breaklines=true |
| 41 |
} |
| 42 |
\end{lstlisting} |
| 43 |
|
| 44 |
This configuration adds a single-line frame around the code, sets a light gray background, and defines styles for keywords, comments, and strings. |
| 45 |
|
| 46 |
\lstinputlisting[caption=This is a caption for the inserted code,style=MatlabStyle]{\insertcode{matlabCode.m}} |
| 47 |
|
| 48 |
\lstinputlisting[caption=This is a caption for the inserted code,style=CStyle]{\insertcode{cCode.cpp}} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
% INSERT EXAMPLE: Include an example of a code listing using this or a similar custom configuration. |
| 53 |
|
| 54 |
\section{Advanced Features} |
| 55 |
|
| 56 |
\subsection{Including External Files} |
| 57 |
|
| 58 |
The \texttt{listings} package allows you to include code from external files. |
| 59 |
This is particularly useful if you have long code files that you want to reference directly. |
| 60 |
|
| 61 |
\begin{lstlisting}[style=LaTeXStyle] |
| 62 |
\lstinputlisting[language=Python]{path/to/your/code.py} |
| 63 |
\end{lstlisting} |
| 64 |
|
| 65 |
Replace \opt{path/to/your/code.py} with the actual path to your file. |
| 66 |
Or you can use the \cmd{addmedia}\mopt{./99_Inclusions/} and \cmd{addcode}\mopt{Code/} commands to define the location for code files, then you can just use \cmd{insertcode}\mopt{filename.ext} command instead of the full path. |
| 67 |
You can customize the display in the same way as inline listings. |
| 68 |
|
| 69 |
% INSERT EXAMPLE: Demonstrate including an external file and discuss any specific considerations, such as relative vs. absolute paths. |
| 70 |
|
| 71 |
\subsection{Handling Special Characters} |
| 72 |
If your code contains special characters (\textit{e.g.}, \texttt{\#}, \texttt{\%}, \texttt{\$}), you may need to escape them or use the \texttt{literate} option to ensure proper display. |
| 73 |
|
| 74 |
\begin{lstlisting}[style=LaTeXStyle] |
| 75 |
\lstset{ |
| 76 |
literate={~} {$\sim$}{1} |
| 77 |
} |
| 78 |
\end{lstlisting} |
| 79 |
|
| 80 |
This command, for example, replaces the tilde symbol with the appropriate LaTeX command. |
| 81 |
|
| 82 |
% INSERT EXAMPLE: Include an example where special characters are handled correctly. |
| 83 |
|
| 84 |
\section{Line Breaks in Long Code Lines} |
| 85 |
|
| 86 |
To automatically break long lines of code, use the \opt{breaklines=true} option as shown in the earlier examples. |
| 87 |
This prevents code from running off the page and maintains readability. |
| 88 |
|
| 89 |
% INSERT EXAMPLE: Provide a code snippet with long lines that demonstrate automatic line breaks. |
| 90 |
\section{Conclusion} |
| 91 |
|
| 92 |
Including well-formatted code listings in your thesis can enhance the clarity of your work and demonstrate your technical skills. |
| 93 |
By following the guidelines in this appendix, you can ensure that your code is presented professionally. |
| 94 |
|
| 95 |
% INSERT FINAL NOTES: Consider adding any university-specific guidelines or final remarks relevant to code listings. |