是否可以定义一个可能不会显示的环境?

是否可以定义一个可能不会显示的环境?

我目前正在改进技术计算机科学的讲义。讲义是打印出来的。这些笔记中的章节从右侧开始,因此可能会有一页完全空白。我不想浪费空间。

我的想法是把这个空间填满练习。我会添加一些可以填满两页的练习,比如,enumerate我希望 LaTeX 会“删除”右侧的练习。

这可能吗?

编辑:

由于问题似乎不够清楚,我制作了一张图片:

在此处输入图片描述

我希望能够写类似这样的内容:

\begin{onlyInsertIfFreeSpace}
    \item What is the answer to life the universe and everything?
    \item Is it possible to define such an environment in LaTeX?
    \item This environment should insert as many items as possible
    \item So I might have 20 items, but the document only displays 6 at the 
          end, because 6 had enough space
\end{onlyInsertIfFreeSpace}

答案1

这是我的尝试。

将此添加到您的序言中:

\usepackage{calc}

\newlength{\alength}
\newlength{\blength}
\newif\ifprinted\printedtrue

\newcommand{\myitem}[1]{%
\setlength{\alength}{\textheight-\the\pagetotal-10pt}
\setlength{\blength}{\totalheightof{\parbox{\linewidth}{#1}}}
\ifprinted
  \ifodd\value{page}
    \ifnum\pagetotal>0
      \ifnum\alength<\blength
        \clearpage
        \item #1
        \printedtrue
      \else
        \item #1
        \printedtrue
      \fi
    \fi
  \else
    \printedfalse
    \ifnum\alength>\blength
      \item #1
      \printedtrue
    \fi
  \fi
\fi
}

\newenvironment{myenumerate}{\begin{enumerate}\item[]}{\end{enumerate}}

新定义的命令将在环境内部\myitem使用,方式如下:\itemmyenumerate

\myitem{What is the answer to life the universe and everything?}

代替

\item What is the answer to life the universe and everything?

定义新环境myenumerate是为了避免在未\myitem打印任何内容时出现错误(它包含“幻影”项目\item[]

\myitem仅当它们位于非空的奇数页中或偶数页中有足够空间时才会打印。

下面的例子可以更好地解释这一点:

\documentclass[openright]{book}
\usepackage{lipsum}
\usepackage{calc}

\newlength{\alength}
\newlength{\blength}
\newif\ifprinted\printedtrue

\newcommand{\myitem}[1]{%
\setlength{\alength}{\textheight-\the\pagetotal-10pt}
\setlength{\blength}{\totalheightof{\parbox{\linewidth}{#1}}}
\ifprinted
  \ifodd\value{page}
    \ifnum\pagetotal>0
      \ifnum\alength<\blength
        \clearpage
        \item #1
        \printedtrue
      \else
        \item #1
        \printedtrue
      \fi
    \fi
  \else
    \printedfalse
    \ifnum\alength>\blength
      \item #1
      \printedtrue
    \fi
  \fi
\fi
}

\newenvironment{myenumerate}{\begin{enumerate}\item[]}{\end{enumerate}}

\begin{document}

\chapter{Introduction}
\lipsum[1-6]

\begin{myenumerate}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
  \myitem{What is the answer to life the universe and everything?}
\end{myenumerate}

\chapter{Some more stuff}
\lipsum

\end{document} 

如您所见,您有 15 个\myitem,但是第 15 个没有打印,因为页面上没有足够的空间。

在此处输入图片描述

稍微玩一下\lipsum你就会发现它在偶数页和奇数页上都能正常工作。

添加ifprinted了,以避免\myitem在前一个尚未打印的情况下打印后续的 。

答案2

一个快速而简单的解决方案:

% if the chapter ends at odd page (right side)
\ifodd\value{page}
   Exercises go here, in one page.
\fi

相关内容