考虑这个最小的例子:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{changepage}
\newenvironment{testenvironment}[2]{
\textbf{#1} \textbf{#2}
% \vspace*{-6pt} % <--- this is a workaround to fix the vertical space generated by adjustwidth
% but it creates another vertical space problem if adjustwidth has contents
\begin{adjustwidth}{8pt}{8pt} % <--- this adds a vertical space
}
{
\end{adjustwidth}
}
\begin{document}
\begin{testenvironment}{Column1}{Column2}
\end{testenvironment}
\begin{testenvironment}{Column1}{Column2}
\end{testenvironment}
\end{document}
它生成了以下内容:
请注意,两行之间有明显的垂直间距。如果我注释环境adjustwidth
(即\begin{adjustwidth}{8pt}{8pt}
和\end{adjustwidth}
),那么它会删除额外的垂直间距。但我想使用adjustwidth
没有垂直间距的功能。上面的注释行adjustwidth
(\vspace*{-6pt}
)是删除垂直间距的变通方法,但如果adjustwidth
里面有文本(即\begin{testenvironment}{Column1}{Column2} hello \end{testenvironment}
),它会产生另一个垂直间距问题。
那么如何去除产生的多余垂直空间呢adjustwidth
?
答案1
\baselineskip
如果环境以垂直模式结束(例如,如果它是空的),您可以删除其中一个空间
\documentclass{article}
% not needed in current latex \usepackage[utf8]{inputenc}
\usepackage{changepage}
\newenvironment{testenvironment}[2]{% dont forget eol
\textbf{#1} \textbf{#2}%
\begin{adjustwidth}{8pt}{8pt}% <--- this adds a vertical space
}
{%
\ifvmode\mbox{}\par\nobreak\vspace{-\baselineskip}\fi
\end{adjustwidth}%
}
\begin{document}
\begin{testenvironment}{Column1}{Column2}%
\end{testenvironment}
\begin{testenvironment}{Column1}{Column2}
testenvironment % no extra space with added text line
\end{testenvironment}
\begin{testenvironment}{Column1}{Column2}
testenvironment % no extra space with added text line
\end{testenvironment}
\begin{testenvironment}{Column1}{Column2}
testenvironment % no extra space with added text line
\end{testenvironment}
\begin{testenvironment}{Column1}{Column2}
testenvironment % no extra space with added text line
\end{testenvironment}
\end{document}
答案2
需要考虑以下两种情况:
% adjustprob.tex Modified MWE from SE 559287
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{changepage}
\newenvironment{testenvironment}[2]{
\textbf{#1} \textbf{#2}
\begin{adjustwidth}{8pt}{8pt} % <--- this adds a vertical space
}
{
\end{adjustwidth}%
}
\begin{document}
\begin{testenvironment}{Column1}{Column2}
% \mbox{ } % doesn't work for some reason
\end{testenvironment}
\vspace{-1em} % remove space after the environment
\begin{testenvironment}{Column1}{Column2}
testenvironment % no extra space with added text line
\end{testenvironment}
\begin{testenvironment}{Column1}{Column2}
testenvironment % no extra space with added text line
\end{testenvironment}
\end{document}
当添加额外的文本时,testenvironment
没有额外的垂直空间。如果没有额外的文本,那么就会有额外的垂直空间,可以通过将\vspace{-1em} after the
testenvironment` 结尾删除。
我不知道为什么\mbox{}
不将其视为额外的不可见文本。