4

I just tried using this https://tex.stackexchange.com/a/83883/143861 for syntax highlighting Python in overleaf

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}

% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{12} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{12}  % for normal

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
morekeywords={self},              % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__},          % Custom highlighting
emphstyle=\ttb\color{deepred},    % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb,                         % Any extra options here
showstringspaces=false
}}


% Python environment
\lstnewenvironment{python}[1][]
{
\pythonstyle
\lstset{#1}
}
{}

% Python for external files
\newcommand\pythonexternal[2][]{{
\pythonstyle
\lstinputlisting[#1]{#2}}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

A Mu µ in the Python environment won't render

\begin{document}
\begin{python}
µ = 5
\end{python}
\end{document}

Due to this error:

Package inputenc Error: Invalid UTF-8 byte "B5.

It renders outside of the python environment.

How can I render a mu inside of a syntax-highlighted Python code block?

1

1 Answer 1

4

You need to declare the character to listings.

I suggest also to use newtxtt that has better symbol coverage than txtt.

\documentclass[a4paper,12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{newtxtt}

% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}

\usepackage{listings}

% Python style for highlighting
\newcommand\pythonstyle{%
  \lstset{
    language=Python,
    basicstyle=\ttfamily,
    morekeywords={self},              % Add keywords here
    keywordstyle=\bfseries\color{deepblue},
    emph={MyClass,__init__},          % Custom highlighting
    emphstyle=\bfseries\color{deepred},    % Custom highlighting style
    stringstyle=\color{deepgreen},
    frame=tb,                         % Any extra options here
    showstringspaces=false,
  }%
}

\lstset{literate={µ}{\textmu}{1}}


% Python environment
\lstnewenvironment{python}[1][]
  {\pythonstyle\lstset{#1}}
  {}

% Python for external files
\newcommand\pythonexternal[2][]{{%
  \pythonstyle
  \lstinputlisting[#1]{#2}%
}}

% Python for inline
\newcommand\pythoninline[1]{{\pythonstyle\lstinline!#1!}}

\begin{document}
\begin{python}
µ = 5
for f
\end{python}
\end{document}

The picture shows that there is a distinctive bold font.

enter image description here

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.