Generallistings: Put language name of frame

LaTeX specific issues not fitting into one of the other forums of this category.
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 »

Code: Select all

\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcblisting{ccode}[1]{title = {Listing \thetcblisting: #1},
listing only, colframe = blue!50!black,
colback = blue!10, enhanced,
}
\newtcblisting{latexcode}[1]{title = {Listing \thetcblisting: #1},
listing only, colframe = yellow!50!black,
colback = yellow!10, enhanced,
}
\begin{document}
\begin{ccode}{Print hello World}
	print ''Hello World''
\end{ccode}
And now we wanna know something else
\begin{latexcode}{Generate a table of contents}
	\tableofcontents
\end{latexcode}
\end{document}

Have a look at the tcolorbox documentation, there is much to discover.

The listings as they are now feel like blobs of stuff, closed within themselves. When i look at it, the caption seems lost in space, as if it were alone. Using different colors, one could imply the kind of code from the color and use the title slot for the caption.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.

Recommended reading 2024:

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

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

CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

listings: Put language name of frame

Post by CThule »

Hi Johannes_B, I actually did come up with the newtcblisting.
My reason for not using different colors and therefore putting the listing in the title slot is, that I have to write a report on about 80 pages using 7-8 different languages. It might be difficult remembering just what color is which language.

Using newtcblisting gives the same error, when I attempt to wrap it in a newcommand.

Her is my newtcblisting:

Code: Select all

\newtcblisting{code}[2]{%
	title = {#1},
	fonttitle=\sffamily,
	listing only, 
	listing remove caption=false, 
	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}
}
My attempt to wrap it in a newcommand (which again gives file ended while scanning for use of \next):

Code: Select all

\newcommand{\codepseudo}[1]{
\begin{code}{Pseudocode}{pseudo}
#1
\end{code}}

\codepseudo{hello}
Johannes_B wrote:

Code: Select all

\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcblisting{ccode}[1]{title = {Listing \thetcblisting: #1},
listing only, colframe = blue!50!black,
colback = blue!10, enhanced,
}
\newtcblisting{latexcode}[1]{title = {Listing \thetcblisting: #1},
listing only, colframe = yellow!50!black,
colback = yellow!10, enhanced,
}
\begin{document}
\begin{ccode}{Print hello World}
	print ''Hello World''
\end{ccode}
And now we wanna know something else
\begin{latexcode}{Generate a table of contents}
	\tableofcontents
\end{latexcode}
\end{document}

Have a look at the tcolorbox documentation, there is much to discover.

The listings as they are now feel like blobs of stuff, closed within themselves. When i look at it, the caption seems lost in space, as if it were alone. Using different colors, one could imply the kind of code from the color and use the title slot for the caption.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: listings: Put language name of frame

Post by Johannes_B »

You are using the wrong approach. Instead of defining a macro that passes pseudocode to a generic environment, you just define different specific environments as i did above.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

listings: Put language name of frame

Post by CThule »

Yes I get that, but then I have to insert my actual code like this:

Code: Select all

\begin{listing}
	\begin{code}{JSON}{json}
		{ "message": "Hello, World!" }
	\end{code}
	\caption{Example of a JSON block}
	\label{lst:json}
\end{listing}
Wrapping both the listing and code inside a newcommand I could just do this:

Code: Select all

\codepseudo{print hello}{lst:pseudo}{Example of a pseudo block}
And it would inserting listings a lot easier. I would still use the created environment.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: listings: Put language name of frame

Post by Johannes_B »

No, it will not be easier as you would note far in the future cursing yourself for using a macro. Trust me.

The thing is, you are working with verbatim stuff, so everything you are trying to do is bound to fail, because it just doesn't work. You cannot do it. It is impossible.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

Re: listings: Put language name of frame

Post by CThule »

Okay. Thank you. I am creating different environments for the different languages now :)
CThule
Posts: 13
Joined: Tue Nov 10, 2015 7:46 pm

listings: Put language name of frame

Post by CThule »

Hi Stefan K. I am having trouble with using the listings and tcb inside a minipage, because minipage is not floating.
Stefan_K wrote: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
Stefan
Post Reply