Text FormattingListing

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Jule_123
Posts: 1
Joined: Thu Aug 11, 2016 9:22 pm

Listing

Post by Jule_123 »

Hello,
i have to include a do-file from stata into my Bachelor Thesis and i wanted to do that with the listing option. Because there is no possibility for do files, i defined a new language by myself.
Everything works now except one thing: i want to color one word after using $ in blue, like this:
Example.JPG
Example.JPG (12.81 KiB) Viewed 3722 times


I tried several possibilities but all i get is either everything is black and nothing is blue or everything between two of this $ is blue, like this:
Example2.JPG
Example2.JPG (13.39 KiB) Viewed 3722 times
I don't know how i can solve this Problem and i would be very thankful if you could help me! :shock: :lol: :)
Thank you in advance!

Here is one of the codes i tried:

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\documentclass[a4paper,12pt,twoside,openright,DIV=15,BCOR25mm]{scrbook}
\KOMAoptions{DIV=last}

\usepackage{listings}
\usepackage{xcolor}
\usepackage{fontspec}

\definecolor{darkgreen}{rgb}{0,0.55,0}
\definecolor{darkblue}{rgb}{0,0,0.55}
\definecolor{blue}{rgb}{0,0,1}
\definecolor{lightblue}{rgb}{0,0.5,0.5}
\definecolor{marron}{rgb}{0.58,0.08,0}

\lstdefinelanguage{Stata}{
morekeywords={generate, label, keep, if,
}, 
morekeywords=[2]{group, 
},
morecomment=[l]{//}, 
morestring=[b]",
morecomment=[n][keywordstyle9]{`}{'},
morestring=[b][\color{lightblue}]\$,
sensitive=true,
}

\lstdefinestyle{numbers}{
	numbers=left,
	stepnumber=1, 
	numberstyle=\tiny, 
	xleftmargin=2em
}

\lstdefinestyle{nonumbers}{
	numbers=none
}

\lstdefinestyle{stata-plain}{
	language=Stata, 
	basicstyle=\setmonofont{Consolas}\scriptsize\ttfamily,
}

\lstdefinestyle{stata-editor}{
	language=Stata, 
	basicstyle=\setmonofont{Consolas}\scriptsize\ttfamily, 
	keywordstyle={\color{darkblue}}, 
	keywordstyle=[2]{\color{blue}},
	keywordstyle=[9]{\color{lightblue}},
	stringstyle=\color{marron},
	commentstyle=\color{darkgreen},
}

\lstset{language=Stata, style=stata-plain, style=numbers, style=stata-editor, showstringspaces=false, breaklines, frame=single,
}

\begin{document}

\begin{lstlisting}
	//Comment 
	"String list" 
	`macro variable'
	generate label keep if 
	group(...)
	$exp_main something between $exp_traffic $exp_XRF
\end{lstlisting}
 
\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Stefan Kottwitz
Site Admin
Posts: 10335
Joined: Mon Mar 10, 2008 9:44 pm

Listing

Post by Stefan Kottwitz »

Hi Jule,

welcome to the forum!

I would do it fast and easy:
  • remove morestring=[b][\color{lightblue}]\$,
  • add morekeywords = [9]{$exp_main,$exp_traffic,$exp_XRF},
for all such keywords. At least it's a quick solution that does it, though you need to list the keywords.

Stefan
LaTeX.org admin
Post Reply