图表和列表。需要在其上方留出相等的垂直空间

图表和列表。需要在其上方留出相等的垂直空间

我有一份文件,其中有几个图表和清单。

有没有办法为所有环境定义统一的间距,尤其是图/表/列表开始之前文本后的垂直间距?我可以将其包装到全局设置中吗?

到目前为止,我\vspace{10pt}在图形、表格、列表开始之前使用,但我不想在每个图/表/列表前面写它。

梅威瑟:

\documentclass{book}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{blindtext}
\usepackage{xcolor}

\begin{document}
\blindtext

\begin{table}[htb]%
  \centering
    \begin{tabular}{|l|l|l|}\hline
        hello & test & ok \\\hline
        its & a long text & for this table \\\hline
    \end{tabular}
  \caption{i am the caption for this table} 
\end{table}

Hello, here is some text without a meaning. This text should show what a
printed text will look like at this place. If you read this text, you will get no information. Really? Is there no information? Is there a difference between this

\begin{lstlisting}
  public class test {
    public static void main(String[] args) {
      System.out.println("test");
      //test
    }
  }
\end{lstlisting}

\blindtext

\begin{figure}[htb]
  \centering
  \fbox{\includegraphics[width=0.5\textwidth]{kaefer.png}}
  \caption{example of a beetle}
\end{figure}

\blindtext

\end{document}

我想将图片中的绿色边框增加 10pt。我之前使用过 vspace{10pt}。但这样做似乎有点不对:/

在此处输入图片描述

答案1

here页面中间的浮动元素周围的空间是\intextsep为了放置

\setlength\intextsep{...}

为文档序言中您需要的任何值。

您可以将列表放在listings浮动位置,这样它将具有相同的间距和标题等。或者您可以使用 aboveskip 和 belowskip 键来列出列表

在此处输入图片描述

\documentclass{book}
\usepackage[english]{babel}
\usepackage{graphicx}
\usepackage{listings}
\usepackage{blindtext}
\usepackage{xcolor}
\setlength\intextsep{1cm plus .5cm minus.2cm}
\begin{document}
\blindtext

\begin{table}[htb]%
  \centering
    \begin{tabular}{|l|l|l|}\hline
        hello & test & ok \\\hline
        its & a long text & for this table \\\hline
    \end{tabular}
  \caption{i am the caption for this table} 
\end{table}

Hello, here is some text without a meaning. This text should show what a
printed text will look like at this place. If you read this text, you will get no information. Really? Is there no information? Is there a difference between this

\begin{lstlisting}[float=htp]
  public class test {
    public static void main(String[] args) {
      System.out.println("test");
      //test
    }
  }
\end{lstlisting}

\blindtext

\begin{figure}[htb]
  \centering
  \fbox{\includegraphics[width=0.5\textwidth]{example-image.png}}
  \caption{example of a beetle}
\end{figure}

\blindtext

\end{document}

答案2

加载etoolbox包后,您可以像这样控制不同环境之前的空间:

\usepackage{etoolbox}% http://ctan.org/pkg/etoolbox

\def\spabove{12pt}
\BeforeBeginEnvironment{figure}{\kern \spabove}%
\BeforeBeginEnvironment{table}{\kern \spabove}%
\BeforeBeginEnvironment{lstlisting}{\kern \spabove}%

编辑

正如@DavidCarlisle 所指出的,使用过程中可能会出现问题kern,因此可以使用\addvspace{x\baselineskip}

\BeforeBeginEnvironment{figure}{\addvspace{2\baselineskip}}%
\BeforeBeginEnvironment{table}{\addvspace{2\baselineskip}}%
\BeforeBeginEnvironment{lstlisting}{\addvspace{2\baselineskip}}%

相关内容