表格内的自定义(或任何)环境会添加不需要的 vspace

表格内的自定义(或任何)环境会添加不需要的 vspace

我想定义自己的类似逐字的环境版本,它具有较少的行间距并使用alltt而不是verbatim,这样我可以\(\cdots\)在里面使用。

我尝试的是:

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

但只要它在段落之间起作用:

图片1

它导致我的(长)表格内部有些混乱。我想在表格单元格内使用它,但它在上方和下方产生了太多垂直空间:

在此处输入图片描述

事实上,我尝试了所有我能找到的方法来摆脱这种情况,但并没有打破表格外的行为。

似乎每次在单元格内使用环境都会产生这样的结果?如何让它表现得像文本,而不是在上方和下方添加空行?

编辑:这是重现问题的最小测试用例:

% -*- 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
 \endfirsthead
\multicolumn{3}{c}{{ \tablename\ \thetable{} -- continuation}} \\ 
 \hline
 \textbf{Field} & \textbf{Descr} & \textbf{Structure} \\ \hline
 \endhead
\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}

答案1

尝试这个:

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

我最近遇到了类似的问题,ignorespacesignorespacesafterend根据额外空间定制 LaTeX为我工作。

更新:这是一个可以运行的版本,但有点儿像黑客。我包含了该{xparse}包,并向环境中添加了两个可选参数codify,允许您指定此环境之前和之后的空间调整。

% -*- 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
 \endfirsthead
\multicolumn{3}{c}{{ \tablename\ \thetable{} -- continuation}} \\ 
 \hline
 \textbf{Field} & \textbf{Descr} & \textbf{Structure} \\ \hline
 \endhead
\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}

相关内容