TeX - LaTeX Asked by Enno on February 27, 2021
I’m currently trying to define my own code style with the Listings package for LaTeX. Among other things this style should display numbers in a separate color. However, if the numbers are within a keyword, an identifier or similar, they should not be changed. I have already found some other similar questions here, but couldn’t solve the problem with any of them. Also, I already asked this question a few days ago, but unfortunately it was closed as a duplicate and the associated question there produces exactly the same as my current MWE. If I missed anything, just let me know.
Many thanks in advance!
Here is an example how it should look like:
And here is a MWE with my current status, but as you can see all numbers are displayed in color:
documentclass{article}
usepackage{listings}
usepackage{xcolor}
definecolor{keyword}{RGB}{0, 0, 255}
definecolor{identifier}{RGB}{0, 0, 0}
definecolor{comment}{RGB}{0, 255, 0}
definecolor{string}{RGB}{255, 128, 0}
definecolor{number}{RGB}{255, 0, 0}
lstdefinestyle{Style}{
language=Python,
keywordstyle=color{keyword},
identifierstyle=color{identifier},
commentstyle=color{comment},
stringstyle=color{string},
literate=*
{0}{{{color{number}{0}}}}{1}
{1}{{{color{number}{1}}}}{1}
{2}{{{color{number}{2}}}}{1}
{3}{{{color{number}{3}}}}{1}
{4}{{{color{number}{4}}}}{1}
{5}{{{color{number}{5}}}}{1}
{6}{{{color{number}{6}}}}{1}
{7}{{{color{number}{7}}}}{1}
{8}{{{color{number}{8}}}}{1}
{9}{{{color{number}{9}}}}{1}
}
begin{document}
begin{lstlisting}[language=Python, style=Style]
import win32gui
pi = 3.1415926
hwnd = win32gui.GetForegroundWindow()
x1, y1, x2, y2 = win32gui.GetWindowRect(hwnd)
center = ((x1 + x2) / 2, (y1 + y2) / 2)
print("Center of the 2 points =", center)
# Center: (10, 10)
end{lstlisting}
end{document}
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP