Generallistings: Put language name of frame

LaTeX specific issues not fitting into one of the other forums of this category.
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

listings: Put language name of frame

Post by CThule »

I am writing a report where I have to use multiple programming languages and I am using the listings package. So far I have used different frames for different languages, but it is getting hard to remember which language uses which frame. An example of how I am doing it can be seen below:

Code: Select all

\lstdefinestyle{pseudo}{
	language=Pseudo,
	frame=single
}
\lstdefinelanguage{Pseudo}{
 	keywords={}, 
 	comment=[l]{}
}
\begin{lstlisting}[style=pseudo, caption={Example of a pseudocode block}, label=lst:json]
print "Hello, World!"
\end{lstlisting}
If it is possible, I would prefer to have the language name written somewhere on the frame.
I was hoping for something like the figure below with e.g. "Pseudocode" instead of "This is a Text in Border":
image.png
image.png (7 KiB) Viewed 8120 times
However, I am not sure how to achieve this, so I help one of you can help or have a great alternative.


Cheers
Last edited by Stefan Kottwitz on Wed Nov 11, 2015 2:24 pm, edited 2 times in total.

Recommended reading 2024:

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

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

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

Re: listings: Put language name of frame

Post by Stefan Kottwitz »

Hi,

welcome to the forum!

Can you please post sample code which shows how you are doing the listings now, with the frames?

Stefan
LaTeX.org admin
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

listings: Put language name of frame

Post by CThule »

Stefan_K wrote:Hi,

welcome to the forum!

Can you please post sample code which shows how you are doing the listings now, with the frames?

Stefan
Thank you, I have updated the post :)
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

listings: Put language name of frame

Post by Stefan Kottwitz »

A quick idea using tcolorbox together with listings, which can be customized - I just used some colors too:

Code: Select all

\documentclass{article}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\begin{document}
\begin{tcblisting}{title = {Pseudocode}, fonttitle=\sffamily\bfseries,
  listing only, label = lst:json, colframe = yellow!50!black,
  colback = yellow!10, enhanced,
  attach boxed title to top left = {xshift=2em, yshift=-1.5ex}}
print "Hello, World!"
\end{tcblisting}
\end{document}
codebox.png
codebox.png (4.45 KiB) Viewed 8119 times
Stefan
LaTeX.org admin
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

listings: Put language name of frame

Post by CThule »

Stefan_K wrote:A quick idea using tcolorbox together with listings, which can be customized - I just used some colors too:

Code: Select all

\documentclass{article}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\begin{document}
\begin{tcblisting}{title = {Pseudocode}, fonttitle=\sffamily\bfseries,
  listing only, label = lst:json, colframe = yellow!50!black,
  colback = yellow!10, enhanced,
  attach boxed title to top left = {xshift=2em, yshift=-1.5ex}}
print "Hello, World!"
\end{tcblisting}
\end{document}
codebox.png
Stefan
It looks awesome, except I cannot get a standard caption below the entire box
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

listings: Put language name of frame

Post by Stefan Kottwitz »

I extended it, defining a new float type with captions for this purpose, which can also generate a list of listings.

Code: Select all

\documentclass{article}
\usepackage{listings}
\usepackage[most]{tcolorbox}
\usepackage{newfloat}
\usepackage{caption}
\DeclareFloatingEnvironment[fileext=lst,placement={!htbp},name=Listing]{listing}
\captionsetup[listing]{labelfont=bf}
\begin{document}
\listoflistings
\begin{listing}
\begin{tcblisting}{title = {Pseudocode}, fonttitle=\sffamily\bfseries,
  listing only, label = lst:json, colframe = yellow!50!black,
  colback = yellow!10, enhanced,
  attach boxed title to top left = {xshift=2em, yshift=-1.5ex}}
print "Hello, World!"
\end{tcblisting}
\caption{A first listing printing ``Hello world!''}
\end{listing}
\end{document}
listings.png
listings.png (13.63 KiB) Viewed 8114 times
Stefan
LaTeX.org admin
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

Re: listings: Put language name of frame

Post by CThule »

Perfect, thank you!
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

listings: Put language name of frame

Post by Johannes_B »

I myself would argue that the caption should be put inside the box.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10345
Joined: Mon Mar 10, 2008 9:44 pm

Re: listings: Put language name of frame

Post by Stefan Kottwitz »

Hmmm... a caption is never inside a figure, and never inside a table, it's kind of an interface between the surrounding text and the float. So I think it should be consistently the same, not sometimes within a box.

Stefan
LaTeX.org admin
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

listings: Put language name of frame

Post by CThule »

I do not think a listing inside the box fits very well with the rest of my report.

I created a new post for the issue below, because this post has been solved.
New post: http://latex-community.org/forum/viewto ... =5&t=26877
I am trying to turn it into a command, but I am having some trouble with this.
My setup is like this:

Code: Select all

\begin{listing}
	\begin{tcblisting}{title = {Pseudocode},
			fonttitle=\sffamily,
			listing only, 
			listing remove caption=false, 
			label = lst:json2, 
			colframe = black,
			colback = white, 
			left=6mm, 
			bottom=-1mm, 
			top=0mm, 
			enhanced,
			boxrule=0.5pt, 
			attach boxed title to top left = {xshift=5mm, yshift=-3mm},
			boxed title style={colback=white, colframe=white}, 
			coltitle=black,
			sharp corners, 
			listing options={style=pseudo}}
		print "Hello, World!"
	\end{tcblisting}
	\caption{Example of a pseudocode block}
\end{listing}
And my attempted command is like this:

Code: Select all

\newcommand{\chnInnerListing}[5]
{
 \begin{listing}
 	\begin{tcblisting}{title = {{#1}},
 			fonttitle=\sffamily,
 			listing only, 
 			listing remove caption=false, 
 			label = {#3}, 
 			colframe = black,
 			colback = white, 
 			left=6mm, 
 			bottom=-1mm, 
 			top=0mm, 
 			enhanced,
 			boxrule=0.5pt, 
 			attach boxed title to top left = {xshift=5mm, yshift=-3mm},
 			boxed title style={colback=white, colframe=white}, 
 			coltitle=black,
 			sharp corners, 
 			listing options={style={#2}}}
{#5}
	\end{tcblisting}
 	\caption{{#4}}
 \end{listing}
}
	
\newcommand{\chnpseudo}[3]
{
\chnInnerListing{Pseudocode}{pseudo}{{#1}}{{#2}}{{#3}}
}

\chnpseudo{lst:pseudo4}{Example of a pseudocode block}{print "Hello, World!"}
Can any of you see what is wrong? I get the error:
File ended while scanning use of \next. \input{chapters/introduction/readingGuide}.

I have tried matching all the {}, and they seem to be right. I have tried removing spaces as well.
Post Reply