Re: [latexusersgroup] Listings - Indent blocks of code

1,244 views
Skip to first unread message

Werner Grundlingh

unread,
Oct 12, 2012, 11:29:40 AM10/12/12
to latexus...@googlegroups.com
On Friday, October 12, 2012, Kenneth Brun Nielsen wrote:
I'm using the listings package to format some source code of a self-invented code language.

In the code, some hierarchical blocks exist, that are defined by keywords. I want to reflect this hierarchy with some formatting using listings if possible

I.e. the code might look like this:

code.vec:
--
command 1
begin include 1
command 2
begin include 2
command 3
end include 2
command 4
end include 1
command 5
--

In the above the block delimiters are: "begin include" and "end include", respectively. When including code.vec in my document (using:  \lstinputlisting[language={[mydialect]mylang}]{./code.vec} ), I want the result to look something like this:

Results:
--
command 1
|begin include 1
|command 2
||begin include 2
||command 3
||end include 2
|command 4
|end include 1
command 5
--

Any possibility to do this? The other formatting (syntax highlightning etc.) has to work also in the indented blocks, but I need some way to do the indentation.

Best regards,
Kenneth

You need to identify the indentation with some character for listings to be able to hook onto. This is typically done using a TAB. Using listings' showtab, tabsize and tab keys, you can now set the presentation of these. I've done up a mock example and attached the output:

\documentclass{article}
\usepackage{listings}% http://ctan.org/pkg/listings
\begin{document}
\lstset{basicstyle=\ttfamily,
  showtabs=true,
  tabsize=2,
  tab=\smash{\rule[-.2\baselineskip]{.4pt}{\baselineskip}\kern.5em}}
\begin{lstlisting}
Some code
begin{some block 1}
do something
begin{some block 2}
do something else
begin{some block 3}
do something really nested
then some more stuff
end{some block 3}
tidy up calculation
end{some block 2}
finish up
end{some block 1}
\end{lstlisting}
\end{document}

Note that you may have to configure your editor to accommodate the use of TABs. For example, I know that TeXnicCenter can be set to transform TABs to spaces.
listings.png

Peter Flynn

unread,
Oct 12, 2012, 11:53:17 AM10/12/12
to latexus...@googlegroups.com
On Fri, Oct 12, 2012 at 2:04 PM, Kenneth Brun Nielsen <kenneth.br...@gmail.com> wrote:
I'm using the listings package to format some source code of a self-invented code language.
[...]

In the above the block delimiters are: "begin include" and "end include", respectively. When including code.vec in my document (using:  \lstinputlisting[language={[mydialect]mylang}]{./code.vec} ),

You can add your own language recognition with the \lstdefinelanguage command: see the examples in the listings package directory (lstlang1,sty, lstlang2,sty, etc) and read the listings documentation.

///Peter

Kenneth Brun Nielsen

unread,
Oct 15, 2012, 3:48:31 AM10/15/12
to latexus...@googlegroups.com

If I understand you correctly, this will require insertion of formatting lines into the source code? I'd like to avoid that and keep source code (self-invented language, that can be understood by old internal tools) without changes. If really necessary, I could probably create a PERL script, that will do the insertion, but I prefer a simpler method if it exists.

Best regards,
Kenneth

Kenneth Brun Nielsen

unread,
Oct 15, 2012, 3:55:20 AM10/15/12
to latexus...@googlegroups.com

I already did create my own language definition. Keyword highlightning etc. works as supposed. But how can I specifically indent lines?

I did read the documentation, but I'm not very experienced in Latex, so the quite short-termed documentation does not give me any ideas how to do this.

I noted the moredelim argument to be something close to what I want,
moredelim=[s][\ttfamily]{[}{]}

In the above example, the text enclosed by '[' and ']' will be tt'ed - how can I indent it instead?

Peter Flynn

unread,
Oct 15, 2012, 3:59:52 AM10/15/12
to latexus...@googlegroups.com

Does the language inhibit you from indenting the source code to start with? Indentation is usually regarded as A Good Thing in source code.
P

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/latexusersgroup/-/S6XWEMxjEX8J.
To post to this group, send email to latexus...@googlegroups.com.
To unsubscribe from this group, send email to latexusersgro...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/latexusersgroup?hl=en.

Kenneth Brun Nielsen

unread,
Oct 15, 2012, 4:15:49 AM10/15/12
to latexus...@googlegroups.com


On Monday, 15 October 2012 09:59:55 UTC+2, Peter wrote:

Does the language inhibit you from indenting the source code to start with? Indentation is usually regarded as A Good Thing in source code.
P

The language is quite simple: no conditional, no code blocks etc. There is really no need for indentation except for in the final presentation, since some blocks of code are inserted from included files - and I want this to be reflected in the final presentation (i.e. latex document)

Please, just take my word, that indentation in this type of code is not appropriate. I want to change the presentation, not the code.

Best regards,
Kenneth

Peter Flynn

unread,
Oct 15, 2012, 5:33:50 AM10/15/12
to latexus...@googlegroups.com
OK, just confirming.

Have you read the documentation for the listings package? It's pretty dense, but there are some useful hints:
  1. p.34 the breakindent and breakautoindent; in combination with the \lstinputlisting command, so that if your code is stable, you can list it in selected blocks by line number, using different indentation for some blocks;
  2. p.50 the \lstdefineformat command, which seems to allow spacing changes according to specific strings
It is probably worth going through this together with the possibility of adding your own language definitions, which would allow you to build the necessary hooks on which to base the formatting.

///Peter


Ole Peter Smith

unread,
Oct 15, 2012, 10:15:46 AM10/15/12
to latexus...@googlegroups.com

perl is probably the way to gain ultimate control of the formatting.

indenting may be achieved by an itemize environment, should be possible to make it not print nothing instead of the bullet, something like '\item[]'!?

0le

Send via Android

--
You received this message because you are subscribed to the Google Groups "LaTeX Users Group" group.
To view this discussion on the web visit https://groups.google.com/d/msg/latexusersgroup/-/eyYJwWW2xngJ.

Kenneth Brun Nielsen

unread,
Oct 16, 2012, 6:12:06 AM10/16/12
to latexus...@googlegroups.com


On Monday, 15 October 2012 11:33:53 UTC+2, Peter wrote:
Have you read the documentation for the listings package? It's pretty dense, but there are some useful hints:
  1. p.34 the breakindent and breakautoindent; in combination with the \lstinputlisting command, so that if your code is stable, you can list it in selected blocks by line number, using different indentation for some blocks;
  2. p.50 the \lstdefineformat command, which seems to allow spacing changes according to specific strings
1) No, the code is not stable. In fact, the main idea of using latex in this context, is that a new report can be generated quickly no matter how many changes we implemented.

2) This looks interesting. But I have to understand LaTex to a higher degree than I do now to boil this up. I know I can just read 400 pages of documentation and do it myself, it's not the right time for that considering my current plans :)
--
My first basic example is:
--
\documentclass{article}
\usepackage{listings}

\begin{document}
\lstdefineformat{veccmd}{START=\newline\string\newline\indent,END=\newline\noindent\string\newline}
\begin{lstlisting}[format=veccmd]
TEST1
START INCLUDE
TEST2
END INCLUDE
TEST3
\end{lstlisting}
Small is beautiful.
\end{document}
--
But it doesn't work:
"! Undefined control sequence.
l.5 \lstdefineformat
                    {veccmd}{\START=\newline\string\newline\indent,END=\newl...
"
--
Best regards,
Kenneth
 
Reply all
Reply to author
Forward
0 new messages