# Custom (or any) environment inside tabular adds unwanted vspace

I wanted to define my own version of a verbatim-like environment, that has some less line spacing and uses alltt instead of verbatim, so what I can use $$\cdots$$ inside.

What I tried was:

\newenvironment{codify}{\begin{spacing}{0.8}\begin{alltt}}{\end{alltt}\end{spacing}\noindent}


But as long as it works between paragraphs:

it causes some mess inside my (long)table. I wanted to use it inside a table cell, but it produces too much vertical space above it and below it:

And I actually tried everything I could find to get rid of that, but not breaking the out-of-a-table behavior.

It seems like every usage of an environment inside a cell produces such results? How can I make it behave like text, not adding empty lines above and below?

Edit: This is the minimal test case that reproduces the problem:

% -*- coding: utf-8 -*-
\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{setspace}
\usepackage{caption}
\usepackage{array}
\usepackage{longtable}
\usepackage{alltt}
\usepackage{multirow}
%\usepackage{polski}

\newenvironment{codify}{\noindent\begin{spacing}{0.8}\begin{alltt}}{\end{alltt}\end{spacing}\noindent\ignorespacesafterend}%

\begin{document}

This is a structure definition:
\begin{codify} node \{
id: integer,
name: string,
children: [ node, $$\cdots$$ ]
\} \end{codify}
with some text directly after the structure, because it's a continuation of the last sentence.

\small
\begin{longtable}{|m{0.16\textwidth}|m{0.45\textwidth}|m{0.32\textwidth}|}
\caption[Some more stuff]{More stuff} \label{tab_conn_fb} \\
\hline
\textbf{Field} & \textbf{Descr} & \textbf{Structure} \\ \hline
\multicolumn{3}{c}{{ \tablename\ \thetable{} -- continuation}} \\
\hline
\textbf{Field} & \textbf{Descr} & \textbf{Structure} \\ \hline
\hline \multicolumn{3}{r}{{continued on the next page}} \\
\endfoot
\hline \hline
\endlastfoot

\hline
\texttt{accounts} & The Facebook apps and pages owned by the current user. This description will take less space than the structure.
& \begin{codify}Object \{
data: [
Object \{
id: string,
category: string,
name: string
\}, $$\cdots$$ ]
\} \end{codify} \\
\hline
\texttt{activities} & The activities listed on the user's profile, e.g. \textit{Towel Day} czy \textit{Janusz A. Zajdel Award} & \begin{codify}Object \{
data: [
Object \{
id: string,
category: string,
create_time: time\-stamp
\}, $$\cdots$$ ]
\} \end{codify} \\
\hline
\texttt{albums} & And this description is higher than the structure. And this description is higher than the structure. And this description is higher than the structure. And this description is higher than the structure. \textbf{And touches the box} & \begin{codify}Object \{
data: [ Album, $$\cdots$$ ]
\} \end{codify} \\
\hline
\end{longtable}
\normalsize

\end{document}

-
Edit you post, add the images and leave them as hyperlinks (removing the ![...] tag). Someone with edit privileges will add these images for you. –  Werner Aug 6 '11 at 16:52

Try this:

\newenvironment{codify}{\noindent\ignorespaces\begin{spacing}{0.8}\begin{alltt}}{\end{alltt}\end{spacing}\noindent\ignorespacesafterend}%


I had a similar problem recently, and ading ignorespaces and ignorespacesafterend based on the section on Extra Space at customizing LaTeX worked form me.

Update: Here is a version that works, but is a bit of a hack. I included the {xparse} package and added two optional arguments to the codify environment that allow you to specify the space adjustment before and after this environment.

% -*- coding: utf-8 -*-
\documentclass{report}
\usepackage[utf8]{inputenc}
\usepackage{setspace}
\usepackage{caption}
\usepackage{array}
\usepackage{longtable}
\usepackage{alltt}
\usepackage{multirow}
%\usepackage{polski}
\usepackage{xparse}

\NewDocumentEnvironment{codify}{O{0.0ex} O{0.0ex}}{\noindent\begin{spacing}{0.8}\begin{alltt}\vskip#1}{\end{alltt}\end{spacing}\vskip#2\noindent\ignorespacesafterend}%

\begin{document}

This is a structure definition:
\begin{codify} node \{
id: integer,
name: string,
children: [ node, $$\cdots$$ ]
\} \end{codify}
with some text directly after the structure, because it's a continuation of the last sentence.

\small
\begin{longtable}{|m{0.16\textwidth}|m{0.45\textwidth}|m{0.32\textwidth}|}
\caption[Some more stuff]{More stuff} \label{tab_conn_fb} \\
\hline
\textbf{Field} & \textbf{Descr} & \textbf{Structure} \\ \hline
\multicolumn{3}{c}{{ \tablename\ \thetable{} -- continuation}} \\
\hline
\textbf{Field} & \textbf{Descr} & \textbf{Structure} \\ \hline
\hline \multicolumn{3}{r}{{continued on the next page}} \\
\endfoot
\hline \hline
\endlastfoot

\hline
\texttt{accounts} & The Facebook apps and pages owned by the current user. This description will take less space than the structure.
& \begin{codify}[-4.0ex][-5.0ex]Object \{
data: [
Object \{
id: string,
category: string,
name: string
\}, $$\cdots$$ ]
\} \end{codify} \\
\hline
\texttt{activities} & The activities listed on the user's profile, e.g. \textit{Towel Day} czy \textit{Janusz A. Zajdel Award} & \begin{codify}[-4.0ex][-5.0ex]Object \{
data: [
Object \{
id: string,
category: string,
create_time: time\-stamp
\}, $$\cdots$$ ]
\} \end{codify} \\
\hline
\texttt{albums} & And this description is higher than the structure. And this description is higher than the structure. And this description is higher than the structure. And this description is higher than the structure. \textbf{And touches the box} & \begin{codify}Object \{
data: [ Album, $$\cdots$$ ]
\} \end{codify} \\
\hline
\end{longtable}
\normalsize

\end{document}

-
Hm, I tried, but it didn't seem to help. Neither did setting \parskip length to 0pt. What does work inside the table is "adding" -2 line heights to vspace, but it actually causes more trouble than it solves, especially outside the table... –  ikari Aug 7 '11 at 17:57
And setting negative parskip almost fixes the table vspace as well, but also produces conflict with text before and after the environment. I wonder, why is there more vertical space added when it is inside longtable, in a cell, than when between usual paragraphs... –  ikari Aug 7 '11 at 18:04
Please provide a full MWE (including \documentclass) that illustrates the problem. –  Peter Grill Aug 7 '11 at 18:49
I have added the test case, even switched from a custom document class (created for master theses) to a more default one. –  ikari Aug 8 '11 at 18:05
Thank you! This actually works as expected, though I replaced the default parameters with the negative one for my own conenience instead of elegance of the solution. Thank you so much :) –  ikari Aug 8 '11 at 20:22
show 1 more comment