In the answer to this question, the following solution is suggested to raise an asterisk:
\documentclass{scrbook}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{listings}
\usepackage{color}
\definecolor{stringColor}{RGB}{42,0.0,255}
\definecolor{keyword1Color}{RGB}{63,127,95}
\definecolor{keyword2Color}{RGB}{63,127,200}
\definecolor{singleLineCommentColor}{RGB}{127,0,85}
\definecolor{multiLineCommentColor}{RGB}{200,0,127}
\lstdefinelanguage{test}{
basicstyle=\linespread{0.83}\small\ttfamily, % Global Code Style
%literate={}{\normalfont{}}1,
morecomment=[l][\color{singleLineCommentColor}]{//},
morecomment=[s][\color{multiLineCommentColor}]{/}{*/},
morestring=[b]",
morestring=[b]',
commentstyle=\color{commentColor},
keywordstyle=[1]{\bfseries\color{keyword1Color}},
keywordstyle=[2]{\bfseries\color{keyword2Color}},
stringstyle=\color{stringColor},
%...
}
\begin{document}
\begin{lstlisting}[language=test]
/* Bad asterisk placement! /
x*3
\end{lstlisting}
\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"2A}{%
\lst@ttfamily
{\raisebox{2pt}{*}}% used with ttfamily
\textasteriskcentered}% used with other fonts
@empty\z@@empty
\makeatother
\begin{lstlisting}[language=test]
/* good asterisk placement! /
x*3
\end{lstlisting}
\end{document}
I noticed two things:
i) The code only works if the line
basicstyle=\linespread{0.83}\small\ttfamily, % Global Code Style
%literate=*{*}{\normalfont{*}}1,
is used in the test environment, which I don't want to use, however, as it seems to change the fonttype. Any way to keep the usual fonttype of lstlisting?
ii) How is it possible to have a "usual" and a raised asterisk in the same lstlisting environment, which is not possible with the solution?
Thanks.
EDIT: I'm afraid there seems to be a bug in the answer: When I write
\begin{lstlisting}[language=test]
test_one, test_two = do_tests()
\end{lstlisting}
the output contains an asterisk where there shouldn't be one!? How can that be?

EDIT 2: While we're getting closer, when using > or <, also the edited solution breaks, I'm afraid:
\documentclass{scrbook}
\usepackage{listings}
\begin{document}
% https://tex.stackexchange.com/questions/654560/follow-up-to-higher-asterisks-in-lstlisting-environment
\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"2A}{%
\lst@ttfamily
{\raisebox{2pt}{}}% used with ttfamily
{\raisebox{1pt}{}}% used with other fonts
}
\lst@ProcessOther{"40}{\textasteriskcentered}% centered asterisk typed as @
@empty\z@@empty
\makeatother
\begin{lstlisting}[language=python, alsoletter={[,]}]
# Regular font
x**3
# Using "at" symbol to print centered asterisk
x@@3
test_one, test_two = do_tests()
x > y
x < y
\end{lstlisting}
\end{document}
How can the asterisk be fixed this time, and why does it occur in the first time? I thought we baked it?
EDIT 3: There seems to be a nasty interference when I want to use nice ~ and a literate, as proposed in the answer:
\documentclass{scrbook}
\usepackage{listings}
\begin{document}
% https://tex.stackexchange.com/questions/17266/how-to-insert-a-nice-tilde-in-a-lstlisting
\lstset{
literate={~} {$\sim$}{1}
}
\makeatletter
\lst@CCPutMacro
\lst@ProcessOther {"2A}{%
\lst@ttfamily
{\raisebox{0pt}{*}}% used with ttfamily
{\raisebox{1pt}{*}}% used with other fonts
}
\@empty\z@\@empty
\makeatother
\begin{lstlisting}[language=python, literate={(*)}{\textasteriskcentered}{1}]
/* Raised and cencetred asterisks */
x**3, x(*)(*)3
x[~mask] = 0
\end{lstlisting}
\end{document}
When I leave out literate={(*)}{\textasteriskcentered}{1}, the output looks like this:
How can I use both the literate and simultaneously have a nice-looking ~?







<and>intoalsoletter=key as we did with a comma, so that you havealsoletter={<,>,[,]}. However, it's a fragile way, since@is a special character and it's used all over the source code and you will have to add toalsoletter=for each character that produces unexpected asterisk. So you'd better give up this approach and use suggestedliterate=approach that's even more flexible. – antshar Aug 26 '22 at 09:19listingsquestions, you can create an additional post, so other can be involved into a help, because it's less likely that people are tracking all the edits in posts. – antshar Aug 26 '22 at 10:42literate=option overrides the global one, so you need to add~change manually to have something likeliterate={~}{$\sim$}{1} {(*)}{\textasteriskcentered}{1}– antshar Aug 26 '22 at 10:42