For my thesis, I needed a code listing with a circled plus (⊕) in a
comment. This is more or less possible with my current setup: using the
mathescape
option of listings
, I can drop into
latex mode in the middle of a listing using
(*@ latex code @*)
. There I can then use plain math mode to
make a circled plus appear:
\begin{lstlisting}[style=java]
// Let's compute: a (*@\color{listingcomments}$\oplus$@*) b
\end{lstlisting}
This looks like:
You might not notice it on first look, but in my opinion, there’s a
problem here: the plus is not slanted! I want this for consistency with
the comment style. After some back and forth with ChatGPT,
encouraging/coercing it by mentioning that I really don’t mind a hacky
solution, it generated the following definition for
\slantedoplus
(formatting mine):
\newcommand{\slantedoplus}{%
\tikz[baseline=(op.base)]%
\node[inner sep=0pt, outer sep=0pt, scale=1, xslant=0.2]%
(op) {$\oplus$};%
}
\begin{lstlisting}[style=java]
// Let's compute: a (*@\color{listingcomments}$\slantedoplus$@*) b
\end{lstlisting}
This looks like:
The definition can still be improved. E.g. I still need to guess the
number to put into the xslant
argument to make it fit with
the style used in this document. Since I don’t expect I’ll change the
listings
style, I don’t mind hardcoding a guess. Also, the
scale=1
argument doesn’t do anything as far as I can tell;
removing it does not seem to change the slanted plus symbol. Finally,
probably this operator is not spaced properly at all in math mode,
because it’s essentially just dumping a tikz picture inline.
This is all acceptable for me: it looks exactly like what I had in mind, and I only need it for some simple formulas.
As I said, it took a few tries to get here. The first iteration of
\slantedplus
ChatGPT suggested was just a rotated
\oplus
; it looked horrible. The second iteration it got
creative with a bunch of latex commands, but when I compiled it in my
own latex editor the command ChatGPT suggested just produced a regular
\oplus
that did not slant. After mentioning this, ChatGPT
generated the definition you see above. You can inspect the conversation
here.
I’m posting this here because I’m not sure posting this on Stack Overflow will be appreciated by the moderators there. Instead, by posting it here I’m hoping the search engines will eventually pick it up and that people who need to slant their operators will find this. In so far it is actually legal for me to do so, the license for the above code snippet is MIT.
Happy LaTeXing!