Math & ScienceListing and matlab-prettifier

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
Math112358
Posts: 1
Joined: Sat Apr 22, 2023 2:02 pm

Listing and matlab-prettifier

Post by Math112358 »

Hi everyone, It's Matthew here. This is my Tex document:

Code: Select all

\documentclass[10pt,a4paper,twoside]{book}

\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[italian]{babel}
\usepackage{xcolor}
\usepackage{graphicx}
\usepackage[
    left=2cm, % inner
    right=2cm, % outer
    top=2cm,
    bottom=2cm,
    %showframe,
]{geometry}

\usepackage{listings}
\lstnewenvironment{MatlabCodeWithListing}
{\lstset{
    language=Matlab,
    basicstyle=\small\ttfamily,
    frame=single,
    keywordstyle=\color[RGB]{0,0,255},
    commentstyle=\color[RGB]{34,139,34},
    stringstyle=\color[RGB]{160,32,240},
    keepspaces,
    morecomment=[l][\color{blue}]{...},
    frameround=tttt,
    backgroundcolor=\color{gray!10!white},
    %rulecolor=\color{gray!20!white}
}}{}

\usepackage{matlab-prettifier}
\lstnewenvironment{MatlabCodeWithPrettifier}
{\lstset{
    language=Matlab,
    basicstyle=\small\ttfamily,
    frame=single,
    keywordstyle=\color[RGB]{0,0,255},
    commentstyle=\color[RGB]{34,139,34},
    stringstyle=\color[RGB]{160,32,240},
    keepspaces,
    morecomment=[l][\color{blue}]{...},
    frameround=tttt,
    backgroundcolor=\color{gray!10!white},
    style=Matlab-editor,
    mlsectiontitlestyle=\bfseries\color{red},
    %rulecolor=\color{gray!20!white}
}}{}

\begin{document}

\chapter*{My errors}

\section*{What I use now}

With the \textit{MatlabCodeWithListing} I have:

\begin{MatlabCodeWithListing}
%% First section
h = L/N;
%{
    comment line
    comment line
%}
x = linspace(0,L,N+1);

%% Second section
L = 1;
N = 100;
A = [1 2 3; ...
     3 5 6];
% Solution
[x,uh] = upwind(L);
\end{MatlabCodeWithListing}

There are three problems: 
\begin{enumerate}
    \item No bold section title 
    \item No $\%\{\quad \%\}$ comment
    \item The box around the code is squared, but it has round corners, so the backgroundcolor overflows the round corners
\end{enumerate}

\section*{What I tried}

So i read about \textit{matlab-prettifier}:

\begin{MatlabCodeWithPrettifier}
%% First section
h = L/N;
%{
    comment line
    comment line
%}
A = [1 2 3; ...
     3 5 6];
\end{MatlabCodeWithPrettifier}

\begin{enumerate}
    \item The title section is not bold (but i could change the color)
    \item The $\%\{\quad \%\}$ comment is correct
    \item The box around the code is as before
    \item I have an extra hookright (and a white line) in the definition of the matrix $A$
\end{enumerate}

\end{document}
With the MatlabCodeWithListing environment I have three problems:

1. No bold section title
2. No %{ %} comment
3. The box around the code is squared, but it has round corners, so the backgroundcolor overflows the round corners

So i read about matlab-prettifier and now:

1. The title section is not bold (but i could change the color)
2. The %{ %} comment is correct
3. The box around the code is as before
4. I have an extra hookright (and a white line) in the definition of the matrix A

Thanks for help guys

Recommended reading 2024:

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

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

kaiserkarl13
Posts: 707
Joined: Tue Mar 25, 2008 5:02 pm

Listing and matlab-prettifier

Post by kaiserkarl13 »

The reason you cannot change the red text to boldface is because there is no bold typewriter text font in the standard distributions. If you run this through xelatex instead of pdflatex, you might see it, though I have to see non-bold TT and bold TT next to each other to be able to differentiate the two---it's not particularly bold.

The overflowing of rounded corners does not happen for me.
Post Reply