Graphics, Figures & TablesHow to display auto-formatted code in document?

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
DryRun
Posts: 8
Joined: Sun May 19, 2019 7:08 pm

How to display auto-formatted code in document?

Post by DryRun »

Hello,

I am new to using LaTeX and i have seen this LaTeX document with this format for C/C++ code. It's mostly just Arduino code and i want to know how can i do the same to display my code with numbered lines and this special font and colour based on code tags? Any ideas? :D
Attachments
latexcode.png
latexcode.png (68.5 KiB) Viewed 3061 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Ijon Tichy
Posts: 640
Joined: Mon Dec 24, 2018 10:12 am

How to display auto-formatted code in document?

Post by Ijon Tichy »

Sorry, but I can no longer participate here as the administrator is trampling on my wishes on one of his other platforms. :cry:
DryRun
Posts: 8
Joined: Sun May 19, 2019 7:08 pm

How to display auto-formatted code in document?

Post by DryRun »

Thank you for the suggestion. I am now using the 'minted' package and i have several pieces of codes in my LaTeX document. Now, i want to have a list of all the pieces of codes listed in my document and show it in the table of contents such that when i click on the title, it takes me to that section, just like for a chapter. So, i am using the command

Code: Select all

\listoflistings
to show the list of codes used throughout my entire document. But there are 2 problems that i'm trying to fix:
1. it comes up with a fixed title: "List of Listings"
2. it shows the list of codes on a separate page.

I found this method of changing the title to something else:

Code: Select all

\renewcommand\listoflistingscaption{List of source codes}
\listoflistings
And this changes the title of the listings but still, the problem of having it all automatically on its own separate page persists. How to fix that?

I created a new .tex file and put this code in it:

Code: Select all

\chapter*{List of source codes}\label{ch:listofsourcecodes}
\addcontentsline{toc}{chapter}{List of Source Codes}
\renewcommand\listoflistingscaption{List of Source Codes}
\listoflistings
This gives the effect that i want but it creates a blank page with the chapter title, followed by another page with the same title and the list of codes.
Last edited by DryRun on Fri Jul 26, 2019 2:44 am, edited 1 time in total.
DryRun
Posts: 8
Joined: Sun May 19, 2019 7:08 pm

How to display auto-formatted code in document?

Post by DryRun »

I also have another problem/question:

At the bottom of each code snippet that i included in my LaTeX document, there is a "Listing 1:", "Listing 2:", etc and i want to change this or remove it, if possible? I can only add a caption to it, but i would like to change it to "Source code" instead of "Listing". Also, how to make the numbers match the chapter/section instead of currently just incrementing integers?

Code: Select all

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}

\usepackage{minted}
\usepackage{xcolor}

\definecolor{LightGray}{gray}{0.9}
%\definecolor{DarkGray}{gray}{0.1}

%\pagecolor{DarkGray}

\usemintedstyle{borland}

%New colors defined below
\definecolor{codegreen}{rgb}{0,0.6,0}
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
\definecolor{codepurple}{rgb}{0.58,0,0.82}
\definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\title{Code Listing}
\author{someone}
\date{ }

\begin{document}

\maketitle

\section{Code examples}

\begin{listing}[ht]
\begin{minted}{python}
import numpy as np
    
def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #to become the incidence matrix
    VT = np.zeros((n*m,1), int)  #dummy variable
    
    #compute the bitwise xor matrix
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1) 

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;
                
                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)
                
                VT = np.zeros((n*m,1), int)
    
    return M
\end{minted}
\caption{Minimal working example}
\label{listing:1}
\end{listing}

\clearpage

%Python code highlighting
\begin{listing}[ht]
\begin{minted}
[
frame=lines,
framesep=2mm,
baselinestretch=1.2,
bgcolor=LightGray,
fontsize=\footnotesize,
linenos
]
{python}

import numpy as np
    
def incmatrix(genl1,genl2):
    m = len(genl1)
    n = len(genl2)
    M = None #to become the incidence matrix
    VT = np.zeros((n*m,1), int)  #dummy variable
    
    #compute the bitwise xor matrix
    M1 = bitxormatrix(genl1)
    M2 = np.triu(bitxormatrix(genl2),1) 

    for i in range(m-1):
        for j in range(i+1, m):
            [r,c] = np.where(M2 == M1[i,j])
            for k in range(len(r)):
                VT[(i)*n + r[k]] = 1;
                VT[(i)*n + c[k]] = 1;
                VT[(j)*n + r[k]] = 1;
                VT[(j)*n + c[k]] = 1;
                
                if M is None:
                    M = np.copy(VT)
                else:
                    M = np.concatenate((M, VT), 1)
                
                VT = np.zeros((n*m,1), int)
    
    return M
\end{minted}
\caption{Example with line numbers enabled}
\end{listing}

\clearpage

The next code will be directly imported from a file:

%Importing code from file
\begin{listing}[ht]
\inputminted{octave}{BitXorMatrix.m}
\caption{Example from external file}
\label{listing:3}
\end{listing}

\clearpage

One-line code formatting also works with minted. For instance, a simple html sample like this:
\mint{html}|<h2>Something <b>here</b></h2>|can be properly 
formatted.

\clearpage

\renewcommand\listoflistingscaption{List of source codes}
\listoflistings

\end{document}
Post Reply