调整文本、方程式和自定义环境的任意组合的垂直间距

调整文本、方程式和自定义环境的任意组合的垂直间距

编辑感谢@pluton 的评论,我完全重写了这个问题。


我有几份文件需要放在两页纸上。有些文件很短,所以不需要做任何事情。但对于其他一些文件,我需要压缩垂直间距。

为此,我创建了一个变量,并\mylength针对每个文档手动调整该变量。我希望\mylength它是任何环境上方和下方的间距,特别equation是自定义环境defitheo

根据@pluton的评论,我进行了整合\addvspace。但仍然有一些问题。

\mylength这是等于 的结果0pt。它旨在检查最小间距。输出几乎没问题,除了:

  • 周围的垂直空间equation出于某些我无法理解的原因,它的行为就像上面和下面都有文字一样(参见上面的等式(2)、上面的等式(4)、下面的等式(5)[我知道两个等式不应该这样写]、上面的等式(6)。

等于:\mylength0pt

0pt


现在,如果我\mylength0pt改为8pt,结果会出现几个问题:

  • 毫无意外,周围的空间equation还是不对
  • 两者之间的间距defi不正确
  • theo当被包围时不居中defi(见定理 4)

等于:\mylength8pt 在此处输入图片描述

代码:

\documentclass[9pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\usepackage[margin=1.cm,top=1.2cm,bottom=1.3cm,headsep=14pt]{geometry}
\usepackage[utf8]{inputenc}  
\usepackage[T1]{fontenc} 
\usepackage{environ}
\usepackage{tikz}
\usepackage{framed}
\usepackage{amsmath,amsfonts,amssymb}

%%%%%%%%% ENVIRONMENTS %%%%%%%%%%

\newenvironment{mycenter}% to control space after and before environment
  {\addvspace{\mylength}\centering}% \begin{mycenter}[<len>]
  {\par\addvspace{\mylength}}% \end{mycenter}

\NewEnviron{theo}[1]{%

  \begin{mycenter}
\begin{tikzpicture}
  \node[rectangle, rounded corners, inner sep=5pt,minimum width=\textwidth,
    text=black, text opacity=1,
    draw=orange, ultra thick, draw opacity=1,
    fill=orange, fill opacity=.3] 
  (box){%
    \begin{minipage}{.95\textwidth}
      \textbf{#1}\qquad\BODY
  \end{minipage}};
\end{tikzpicture}
\end{mycenter}
}

% for adjustwidth environment
\usepackage[strict]{changepage}

% for definitions
\definecolor{defishade}{rgb}{0.97,0.97,.99}
\newenvironment{defi}{%
\vspace{-1.2mm} % UGLY MANUAL ADJUSTEMENT
\par\addvspace{\mylength}
\setlength{\topsep}{0pt}
  \def\FrameCommand{%
    \hspace{1pt}%
    {\color{black!70}\vrule width 2pt}%
    {\color{defishade}\vrule width 4pt}%
    \colorbox{defishade}%
  }%
  \MakeFramed{\advance\hsize-\width\FrameRestore}%
  \noindent\hspace{-4.55pt}% disable indenting first paragraph
    % \vspace{-8pt}  
  \begin{adjustwidth}{0pt}{7pt}%
  \vspace{1.5pt}\vspace{2pt}%
 \textcolor{black!70}{ \textbf{Definition}\qquad}
}
{%
  \vspace{2pt}
  \end{adjustwidth}\endMakeFramed%
  \vspace{.5mm}
  \addvspace{\mylength}
}




\begin{document}\thispagestyle{empty}
\def\mylength{0pt}
\setlength{\belowdisplayskip}{\mylength} \setlength{\belowdisplayshortskip}{\mylength}
\setlength{\abovedisplayskip}{\mylength} \setlength{\abovedisplayshortskip}{\mylength}


\paragraph{Environment theo}  First, let's check this environment.


\begin{theo}{Theorem} If blah blah blah then
\begin{equation}
1+1=2
\end{equation}
\end{theo}
Everything's fine here.
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
Everything's fine here.
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
\begin{equation}1-1=0\end{equation}
\textcolor{red}{\bf Why is the distance between the above equation and the theorem so large?}

\paragraph{Environment defi}  Now, let's check this second environment. 
\begin{defi}Below, it's OK for \verb?0pt? and \textcolor{red}{too large} for \verb?8pt?.\end{defi}
\begin{defi}Nothing special.\end{defi}
Spacing is OK for 0pt but not centered (vertically) as between the two first theorems for 10pt.\begin{defi}Here, it's OK.\end{defi}
\begin{theo}{Theorem 2} If blah blah blah then
\begin{equation}
1+1=2
\end{equation}
\end{theo}
\begin{equation}
2+3=5 \text{ when two environments are following eachother, the vertical spacing adds up.}  
\end{equation}
\begin{equation}
1+1=2
\end{equation}%
%qsdfqsdf
\begin{defi}\textcolor{red}{\bf Why is the distance between the above equation and the present, and previous definitions, so large?}
\end{defi}
\begin{theo}{Theorem 4} \textcolor{red}{Theorem is not centered between two definitions for 8pt.}
\end{theo}%
\begin{defi}\textcolor{red}{Space too large below for 10pt.}\end{defi}
\begin{equation}
1+1=2%
\end{equation}%
%sdfqssddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
\begin{defi}Kombucha is a bloh bloh bloh.\end{defi}
\end{document}

答案1

  • 重新定义造成一些麻烦的环境mycenter

    \newenvironment{mycenter}%
    {\parskip=0pt\par\addvspace{\mylength}\centering}%
    {\par\addvspace{\mylength}\noindent\ignorespacesafterend}%
    
  • defi使用tikzas重新定义自定义环境theo,以便我能够理解代码,

  • \mylength按照 Sigur 和 elford 的建议改为可延长的长度\mylength{8pt plus 4pt minus 6pt}
  • 并使用\addvspace以避免两次添加垂直空间,

我能够制作出符合我需求的产品:

在此处输入图片描述

完整代码:

\documentclass[9pt,a4paper]{article}
\usepackage[table]{xcolor}
\usepackage{graphicx}
\usepackage[margin=1.cm,top=1.2cm,bottom=1.3cm,headsep=14pt]{geometry}
\usepackage[utf8]{inputenc}  
\usepackage[T1]{fontenc} 
\usepackage{environ}
\usepackage{tikz}
\usepackage{framed}
\usepackage{amsmath,amsfonts,amssymb}

%%%%%%%%% ENVIRONMENTS %%%%%%%%%%

\newenvironment{mycenter}% to control space after and before environment
  {\parskip=0pt\par\addvspace{\mylength}\centering  }% \begin{mycenter}[<len>]
  {\par\addvspace{\mylength}\noindent\ignorespacesafterend}% \end{mycenter}

\NewEnviron{theo}[1]{%

  \begin{mycenter}
\begin{tikzpicture}
  \node[rectangle, rounded corners, inner sep=5pt,minimum width=\textwidth,
    text=black, text opacity=1,
    draw=orange, ultra thick, draw opacity=1,
    fill=orange, fill opacity=.3] 
  (box){%
    \begin{minipage}{.95\textwidth}
      \textbf{#1}\qquad\BODY
  \end{minipage}};
\end{tikzpicture}
\end{mycenter}
}

\definecolor{defishade}{rgb}{0.97,0.97,.99}
\NewEnviron{defi}{%
  \begin{mycenter}
\begin{tikzpicture}
  \node[rectangle, inner sep=5pt,minimum width=\textwidth,
    text=black, text opacity=1,
    draw=none, ultra thick, draw opacity=1,
    fill=defishade, fill opacity=1,
    line width=0pt,
    append after command={
      (\tikzlastnode.north west) edge [line width=.8mm,color=black!70]
(\tikzlastnode.south west)}] 
  (box){%
    \begin{minipage}{.95\textwidth}
      \textcolor{black!70}{\textbf{Définition}}\qquad\BODY
  \end{minipage}};
\end{tikzpicture}
\end{mycenter}
}


\begin{document}\thispagestyle{empty}
\def\mylength{8pt plus 4pt minus 6pt}
\setlength{\belowdisplayskip}{\mylength} \setlength{\belowdisplayshortskip}{\mylength}
\setlength{\abovedisplayskip}{\mylength} \setlength{\abovedisplayshortskip}{\mylength}


First, let's check this environment.


\begin{theo}{Theorem} If blah blah blah then
\begin{equation}
1+1=2
\end{equation}
\end{theo}
Everything's fine here.
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
Everything's fine here.
\begin{theo}{Theorem} If blah blah blah then nothing.
\end{theo}%
\begin{equation}1-1=0\end{equation}
text text text


 Now, let's check this second environment. 
\begin{defi}A fool is a pool with an \textit{f}.\end{defi}
\begin{defi}Nothing special.\end{defi}
I can write whatever I want, who cares.
\begin{defi}You need to learn all definitions.\end{defi}
\begin{theo}{Theorem 2} If blah blah blah then
\begin{equation}
1+1=2
\end{equation}
\end{theo}
\begin{equation}
\cos \pi=-1
\end{equation}
\begin{equation}
1+1=2
\end{equation}%
\begin{defi} \LaTeX\  is your friend.
\end{defi}
\begin{theo}{Theorem 4} Let's all love each other.
\end{theo}%
\begin{defi}Someone nasty is not nice.\end{defi}
\begin{equation}
1+1=2%
\end{equation}%
\begin{defi}Kombucha is a bloh bloh bloh.\end{defi}
\end{document}

相关内容