I'm using lstnewenvironment
from the listings
package to create new code blocks.
Using an example from its manual:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstnewenvironment{pascal}
{\hfill pascal \lstset{language=pascal}}
{}
\begin{document}
some text before code block
\begin{pascal}
for i := maxint to 0 do
begin
{ do nothing}
end;
\end{pascal}
\end{document}
This generates a pascal
code block like:
enter image description here
Now I want to add some text as 'tag' above the code line (at the end of that line) like:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstnewenvironment{pascal}
{\hfill{\tiny pascal} \lstset{language=pascal}}
{}
This generates: enter image description here
The tag text is at the same line with previous text, so I add \\
before the tag:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstnewenvironment{pascal}
{ \\ \hfill{\tiny pascal} \lstset{language=pascal}}
{}
And tried
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstnewenvironment{pascal}
{ \\ \hspace{\textwidth}{\tiny pascal} \lstset{language=pascal}}
{}
But both of the hfill
and \hspace{\textwidth}
don't work, the tag appears at the beginning of the line above:
enter image description here
The only way I can do is manually adding a blank line between the code block and previous text:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstnewenvironment{pascal}
{ \hfill{\tiny pascal} \lstset{language=pascal}}
{}
\begin{document}
some text before code block
\begin{pascal}
for i := maxint to 0 do
begin
{ do nothing}
end;
\end{pascal}
\end{document}
Output: enter image description here
My question is how to modify the \lstnewenvironment
settings so that it can automatically add a new line between the code block and the previous text, with a tag at the end of that new line?
see https://tex.stackexchange.com/a/625934/36296
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\lstnewenvironment{pascal}
{
\endgraf {\hfill \tiny pascal} \lstset{language=pascal}}
{}
\begin{document}
some text before code block
\begin{pascal}
for i := maxint to 0 do
begin
{ do nothing}
end;
\end{pascal}
some text before code block
\begin{pascal}
for i := maxint to 0 do
begin
{ do nothing}
end;
\end{pascal}
\end{document}