The listings
package automatically highlights keywords in source code and makes it look nice. Is it possible to automatically indent the program? For example, in listings
, if the following code is typed,
#include<stdio.h>
void main()
{
printf("\n Hello World");
}
the output should be
#include<stdio.h>
void main()
{
printf("\n Hello World");
}
Can listings
to do that, and if so, how should I configure it?
Best Answer
The
listings
package provides that functionality; see subsection 5.6 of the manual. Make sure theformats
aspect is loaded, which can be done either by passingformats
as a package option tolistings
,or by using the
\lstloadaspects
macro after loading thelistings
package,Then, use the
\lstdefineformat
macro to define a "format", i.e. instructions about whatlistings
should do upon encountering certain characters. The format definition shown below is taken straight from the manual.Be aware that this feature remains experimental; according to the manual,