0

How do I get background colors assigned to certain words and punctuation marks? I want it like in the following picture: enter image description here

Here is my code:

\documentclass[a4paper]{article}

\usepackage{tikz} \usetikzlibrary{arrows.meta} \usepackage{listings} \usepackage{xcolor}

\def\tikzmark#1{\tikz[remember picture,overlay]\nodeyshift=2pt{};}

\definecolor{codegreen}{rgb}{0,0.6,0} \definecolor{codegray}{rgb}{0.5,0.5,0.5} \definecolor{codepurple}{rgb}{0.58,0,0.82} \definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{ escapeinside={}{}, backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\tiny\color{codegray}, stringstyle=\color{codepurple}, basicstyle=\ttfamily\footnotesize, breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false, showtabs=false,
tabsize=2 }

\lstset{style=mystyle}

\usepackage[absolute,overlay]{textpos} \newcommand\PlaceText[3]{% \begin{textblock}{10in}(#1,#2) %% change width of box from 10in as you wish #3 \end{textblock} }%

\begin{document}

\begin{lstlisting}[language=Python]

Here is the function:

def hello():\tikzmark{A} print("Hello together!")

Main:

hello() \tikzmark{B} hello() \tikzmark{C} hello() \tikzmark{D} \end{lstlisting} \tikzset{Empharrow/.style={dashed,red,thick,->,arrows={-Triangle[width=3pt]}}} \begin{tikzpicture}[remember picture,overlay] \drawEmpharrow--+(7cm,0)|-(A); \drawEmpharrow--+(6cm,0)|-(A); \drawEmpharrow--+(5.5cm,0)|-(A); \end{tikzpicture}

\PlaceText{100mm}{60mm}{The \texttt{hello()} function is called here}

\end{document}

Output:

enter image description here

Is there a more elegant way? Nonetheless, my main concern is the background color of certain words in the code.

wayne
  • 613

1 Answers1

1

Maybe you're looking for this:

use arrows to point to code

In this case, you just have to edit your tikzpicture like the following, and delete your PlaceText line.

Everything else is OK.

\begin{tikzpicture}[remember picture,overlay]
\draw[Empharrow](D)--+(7cm,0)|-(A.north east)node[pos=0.25,right,text width=4cm]{The \texttt{hello()} function\\is called here};
\draw[Empharrow](C)--+(6cm,0)|-(A);
\draw[Empharrow](B)--+(5.5cm,0)|-(A.south east);
\end{tikzpicture}
SebGlav
  • 19,186
  • Thanks! But how do I get background colors assigned to certain words and punctuation marks? – wayne May 14 '23 at 18:53
  • Sorry I misread your question. It was about background colours. I'll try and add this tomorrow. It can be done with tikzmark library if you don't want to automatize it. Otherwise, you'll have to find a way to add backgrounds on listings, which I don't know how to do... yet. – SebGlav May 14 '23 at 21:24
  • I did some research and only found this article explaining how to make tikzmark and listings libraries work together, but it's not possible to colour some words this way. Not sure this is the best way to do this. Maybe Andrew Stacey could give some help on that. – SebGlav May 15 '23 at 17:32
  • @Andrew Stacey Do you have an idea? – wayne May 17 '23 at 14:03