环境中的幻影

环境中的幻影

我想显示一个相当于文本(或图形)所需空间的框。我已经有下面的代码,但我似乎找不到\phantom在环境中使用以使 boxedminipage 具有正确高度的方法。

\usepackage{boxedminipage}
...
\newenvironment{onblackboard}
{\begin{boxedminipage}{\linewidth}}
{\end{boxedminipage}}}

使用白色字体\color{white}不是一个选择,因为有时我在环境中有图形,我也不想显示。

有任何想法吗?

答案1

环境内容可以使用\onblackboardbox环境存储在一个框()中lrbox。由于此环境使用水平模式,因此环境minipage切换回垂直模式。我们通过获取内部宽度\hsize,环境boxedminipage不会更新\linewidth。然后通过仅使用内容尺寸的框进行设置\phantom,内容本身未设置:

\documentclass{article}
\usepackage{boxedminipage}

\newsavebox\onblackboardbox

\newenvironment{onblackboard}{%
  \begin{boxedminipage}{\linewidth}%
    \begin{lrbox}{\onblackboardbox}%
      \begin{minipage}{\hsize}% boxedminipage does not update \linewidth
}
{%
      \end{minipage}%
    \end{lrbox}%
    \phantom{\usebox{\onblackboardbox}}%
  \end{boxedminipage}%
}

\begin{document}
  \noindent
  \begin{onblackboard}
    Hello World
  \end{onblackboard}
\end{document}

答案2

您不需要boxedminipage,而且很容易提供显示文本的 *-variant:

\documentclass{article}
\usepackage{calc}
\usepackage{lipsum} % just for the example

\newsavebox\onblackboardbox
\newif\ifshowtext

\newenvironment{onblackboardinner}
 {\par\addvspace{\topsep}% some vertical space
  \begin{lrbox}{\onblackboardbox}
  \begin{minipage}{\linewidth-2\fboxsep-2\fboxrule}}
 {\end{minipage}%
  \end{lrbox}%
  \noindent % now typeset the box
  \ifshowtext
    \fbox{\usebox{\onblackboardbox}}%
  \else
    \fbox{\phantom{\usebox{\onblackboardbox}}}%
  \fi
  \par\addvspace{\topsep}
}

% onblackboard doesn't show the text
\newenvironment{onblackboard}
 {\showtextfalse\onblackboardinner}
 {\endonblackboardinner}

% onblackboard* shows the text
\newenvironment{onblackboard*}
 {\showtexttrue\onblackboardinner}
 {\endonblackboardinner}

\begin{document}

\lipsum[2]

\begin{onblackboard}
  Hello World\\
  two lines\\
  three
\end{onblackboard}

\lipsum[3]

\begin{onblackboard*}
  Hello World\\
  two lines\\
  three
\end{onblackboard*}

\end{document}

在此处输入图片描述

相关内容