2

Does anyone know how to remove the whitespace which is created by TeX everytime I use my own created environment? I tried the tricks with \ignorespaces and \ignorespacesafterend but that doesn't seem to work. I want to make use of labels to cross-reference to included code parts later on, and I have a feeling that this labeling issue might be responsible for the white-space I'm generating? Thanks in advance.

\document{book}
\usepackage{listings}
\newcounter{codeexcerptcounter}
\newcommand{\CEN}{Code excerpt~\thecodeexcerptcounter}

\newenvironment{codeexcerpt}[1][]{
\refstepcounter{codeexcerptcounter}%
}{%
}

\newcommand{\addcodeexcerpt}[1][]%
{\begin{center}
\begin{codeexcerpt}
#1
\end{codeexcerpt}
\end{center}}%

\begin{document}

\chapter{First Chapter}

Here goes some explanation.

\addcodeexcerpt[\label{codeex:first}]
\lstinputlisting[title=\CEN: Mirror.java]{code/Mirror.java}

\end{document}
4
  • Which space do you mean in particular? The vertical space generated by the center environment?
    – Johannes_B
    Commented Aug 31, 2015 at 14:02
  • 4
    Instead of \begin{center} and \end{center}, use \begingroup\centering and \endgroup respectively.
    – user11232
    Commented Aug 31, 2015 at 14:06
  • Welcome to TeX.SX! Commented Aug 31, 2015 at 14:14
  • Thanks, this already reduces the amount of whitespace a little. But still, the space above the environment is considerably large. Ideally, it would just be something of the size of a normal linebreak, now it's about 3 times this size.
    – PDB
    Commented Aug 31, 2015 at 14:15

1 Answer 1

6

You are adding lots of space, so just don't add it. For example

\newenvironment{codeexcerpt}[1][]{
\refstepcounter{codeexcerptcounter}%
}{%
}

should be

\newenvironment{codeexcerpt}[1][]{%
\refstepcounter{codeexcerptcounter}%
}{%
}

to avoid adding a space at the start of the environment.

also in

\newcommand{\addcodeexcerpt}[1][]%
{\begin{center}
\begin{codeexcerpt}
#1
\end{codeexcerpt}
\end{center}}%


\addcodeexcerpt[\label{codeex:first}]

The content of the displayed codeexcerpt environment is just \label{codeex:first} which is no visible output and will just look like vertical space on the page.

So something like

\documentclass{book}
\usepackage{listings}
\newcounter{codeexcerptcounter}
\newcommand{\CEN}{Code excerpt~\thecodeexcerptcounter}



\newcommand{\addcodeexcerpt}[2][]%
{\par\refstepcounter{codeexcerptcounter}#1#2}

\begin{document}

\chapter{First Chapter}

Here goes some explanation.

\addcodeexcerpt[\label{codeex:first}]
{\lstinputlisting[title=\CEN: Mirror.java]{xxx.txt}}

\end{document}
1
  • Thanks a lot, David! This seems to have solved the issue. I typically don't create my own environments so wasn't aware of this.
    – PDB
    Commented Aug 31, 2015 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .