序言中的边距环境

序言中的边距环境

我一直在使用在这里其他人的帮助下获得的代码。

\documentclass{book}
\usepackage[many]{tcolorbox}
\usepackage{changepage}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage{marginnote}

\definecolor{framerule}{RGB}{183,41,41}
\colorlet{tryit}{green!70!black}

\newcommand\RedBox{\textcolor{framerule}{$\square$}}

\newtcolorbox{eoexample}{
  enhanced,
  breakable,
  check odd page,
  toggle left and right,
  boxrule=0pt,
  colback=white,
  arc=0pt,
  outer arc=0pt,
  top=\topsep,
  bottom=\topsep,
  left=0pt,
  right=0pt,
  boxsep=0pt,
  overlay unbroken ={%
    \ifoddpage
      \node[draw,tryit,fill=white]
      (tryit) 
      at ([xshift=\marginparsep+0.5\marginparwidth]frame.north east) 
      {You Try It!};
      \draw[framerule] 
        (frame.north west) -- ([xshift=-3pt]tryit.west);
      \draw[framerule]
        ([xshift=3pt]tryit.east) -- 
        ([xshift=\marginparsep+\marginparwidth]frame.north east);
      \node[inner sep=0pt, anchor=east]
        (box)
        at ([xshift=\marginparsep+\marginparwidth]frame.south east)
        {\RedBox};  
      \draw[framerule] 
        (frame.south west) -- 
        ([xshift=-3pt]box.west);
    \else
      \node[draw,tryit,fill=white]
      (tryit) 
      at ([xshift=-\marginparsep-0.5\marginparwidth]frame.north west) 
      {You Try It!};
      \draw[framerule] 
        (frame.north east) -- ([xshift=3pt]tryit.east);
      \draw[framerule]
        ([xshift=-3pt]tryit.west) -- 
        ([xshift=-\marginparsep-\marginparwidth]frame.north west);
      \node[inner sep=0pt, anchor=east, outer sep=0pt]
        (box)
        at (frame.south east)
        {\RedBox};  
      \draw[framerule] 
        ([xshift=-\marginparsep-\marginparwidth]frame.south west) -- 
        ([xshift=-3pt]box.west);
    \fi
  },
  overlay first={%
    \ifoddpage
      \node[draw,tryit,fill=white]
      (tryit) 
      at ([xshift=\marginparsep+0.5\marginparwidth]frame.north east) 
      {You Try It!};
      \draw[framerule] 
        (frame.north west) -- ([xshift=-3pt]tryit.west);
      \draw[framerule]
        ([xshift=3pt]tryit.east) -- 
        ([xshift=\marginparsep+\marginparwidth]frame.north east);
    \else
      \node[draw,tryit,fill=white]
      (tryit) 
      at ([xshift=-\marginparsep-0.5\marginparwidth]frame.north west) 
      {You Try It!};
      \draw[framerule] 
        (frame.north east) -- ([xshift=3pt]tryit.east);
      \draw[framerule]
        ([xshift=-3pt]tryit.west) -- 
        ([xshift=-\marginparsep-\marginparwidth]frame.north west);
    \fi
  },
  overlay last={%
    \ifoddpage
      \node[inner sep=0pt]
        (box)
        at ([xshift=\marginparsep+\marginparwidth]frame.south east)
        {\RedBox};  
      \draw[framerule] 
        (frame.south west) -- 
        ([xshift=-3pt]box.west);
    \else
      \node[inner sep=0pt, anchor=east, outer sep=0pt]
        (box)
        at (frame.south east)
        {\RedBox};  
      \draw[framerule] 
        ([xshift=-\marginparsep-\marginparwidth]frame.south west) -- 
        ([xshift=-3pt]box.west);
    \fi
  }
}

\newtheoremstyle{myexa}% name
  {\topsep}%Space above
  {\topsep}%Space below
  {}%Body font
  {}%Indent amount 1
  {\bfseries}% Theorem head font
  {.}%Punctuation after theorem head
  {.5em}%Space after theorem head 2
  {}%Theorem head spec

\theoremstyle{myexa}

\newtheorem{exa}{Example}

\newenvironment{example}[1][]
  {\begin{eoexample}\begin{exa}}
  {\end{exa}\end{eoexample}}  

\begin{document}


\lipsum[1-4]

\begin{example}
This is a worked out example.\\
\marginnote{\flushleft{\color{framerule}{
Simplify $i^{100}$.}}}[-.75cm]

The solution is typed here, which may go from one page to the next.  The margin can be used to include tables, graphs, or a similar example with answer (but no solution). The question should be right under the line. The answer is above the second line, all in the margin.\\
\lipsum[1] 
\marginnote{\flushleft{\color{framerule}{Answer: 1.}}}[-1.2cm]
\end{example}

\lipsum[1]

\begin{example}
This is a second example. \\
\marginnote{\flushleft{\color{framerule}{
Simplify $i^{101}$.}}}[-.75cm]

Include the solution here. 
\lipsum[1]
\marginnote{\flushleft{\color{framerule}{Answer: $i$.}}}[-1.2cm]
\end{example}
\end{document}

现在,为了在页边空白处输入文本,我必须使用 \marginnote 命令并调整行末的位置。我能否将此代码放在 eoexample 代码中的序言中?

先感谢您。

在此处输入图片描述

在此处输入图片描述

答案1

您希望自动处理顶部和底部的边距注释吗?

example假设如此,我会用以下代码替换你的环境定义

\usepackage{xparse}
\NewDocumentEnvironment{example}{oo}
  {\begin{eoexample}\begin{exa}%
    \IfNoValueF{#1}{\marginnote{\flushleft{\color{framerule}{#1}}}[-7.5mm]}}%
  {\IfNoValueF{#2}{\marginnote{\flushleft{\color{framerule}{#2}}}[-12mm]}%
   \end{exa}\end{eoexample}}

此环境现在有两个可选参数,分别用于顶部和底部边距注释,因此您可以这样写:

\begin{example}[Simplify $i^{100}$][Answer 1]
This is a worked out example.\\

The solution is typed here, which may go from one page to the next.  The margin can be used to include tables, graphs, or a similar example with answer (but no solution). The question should be right under the line. The answer is above the second line, all in the margin.\\
\lipsum[1]
\end{example}

编辑 OP 注意到,当答案超过一行时,它的位置不正确。实际上应该可以自动满足这一要求,但我还没有找到一个好的方法。相反,我发现了一个 hack,它提供了一种简单的方法来调整这一点,即在示例环境中添加另一个可选参数:

\NewDocumentEnvironment{example}{ooO{12}}
  {\begin{eoexample}\begin{exa}%
    \IfNoValueF{#1}{\marginnote{\flushleft{\color{framerule}{#1}}}[-7.5mm]}}%
  {\IfNoValueF{#2}{\marginnote{\flushleft{\color{framerule}{#2}}}[-#3mm]}%
   \end{exa}\end{eoexample}}

对于单行答案,您可以像以前一样使用,但对于多行答案,可以使用第三个可选参数来正确调整框的位置:

\begin{example}[Simplify $i^{101}$][Answer: $i$ that is really really really long][18]
This is a second example.

Include the solution here.
\lipsum[1]
\end{example}

这给出了所需的输出:

在此处输入图片描述

完整代码如下:

\documentclass{book}
\usepackage[many]{tcolorbox}
\usepackage{changepage}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage[depthadjust]{marginnote}

\definecolor{framerule}{RGB}{183,41,41}
\colorlet{tryit}{green!70!black}

\newcommand\RedBox{\textcolor{framerule}{$\square$}}

\newtcolorbox{eoexample}{
  enhanced,
  breakable,
  check odd page,
  toggle left and right,
  boxrule=0pt,
  colback=white,
  arc=0pt,
  outer arc=0pt,
  top=\topsep,
  bottom=\topsep,
  left=0pt,
  right=0pt,
  boxsep=0pt,
  overlay unbroken ={%
    \ifoddpage
      \node[draw,tryit,fill=white]
      (tryit)
      at ([xshift=\marginparsep+0.5\marginparwidth]frame.north east)
      {You Try It!};
      \draw[framerule]
        (frame.north west) -- ([xshift=-3pt]tryit.west);
      \draw[framerule]
        ([xshift=3pt]tryit.east) --
        ([xshift=\marginparsep+\marginparwidth]frame.north east);
      \node[inner sep=0pt, anchor=east]
        (box)
        at ([xshift=\marginparsep+\marginparwidth]frame.south east)
        {\RedBox};
      \draw[framerule]
        (frame.south west) --
        ([xshift=-3pt]box.west);
    \else
      \node[draw,tryit,fill=white]
      (tryit)
      at ([xshift=-\marginparsep-0.5\marginparwidth]frame.north west)
      {You Try It!};
      \draw[framerule]
        (frame.north east) -- ([xshift=3pt]tryit.east);
      \draw[framerule]
        ([xshift=-3pt]tryit.west) --
        ([xshift=-\marginparsep-\marginparwidth]frame.north west);
      \node[inner sep=0pt, anchor=east, outer sep=0pt]
        (box)
        at (frame.south east)
        {\RedBox};
      \draw[framerule]
        ([xshift=-\marginparsep-\marginparwidth]frame.south west) --
        ([xshift=-3pt]box.west);
    \fi
  },
  overlay first={%
    \ifoddpage
      \node[draw,tryit,fill=white]
      (tryit)
      at ([xshift=\marginparsep+0.5\marginparwidth]frame.north east)
      {You Try It!};
      \draw[framerule]
        (frame.north west) -- ([xshift=-3pt]tryit.west);
      \draw[framerule]
        ([xshift=3pt]tryit.east) --
        ([xshift=\marginparsep+\marginparwidth]frame.north east);
    \else
      \node[draw,tryit,fill=white]
      (tryit)
      at ([xshift=-\marginparsep-0.5\marginparwidth]frame.north west)
      {You Try It!};
      \draw[framerule]
        (frame.north east) -- ([xshift=3pt]tryit.east);
      \draw[framerule]
        ([xshift=-3pt]tryit.west) --
        ([xshift=-\marginparsep-\marginparwidth]frame.north west);
    \fi
  },
  overlay last={%
    \ifoddpage
      \node[inner sep=0pt]
        (box)
        at ([xshift=\marginparsep+\marginparwidth]frame.south east)
        {\RedBox};
      \draw[framerule]
        (frame.south west) --
        ([xshift=-3pt]box.west);
    \else
      \node[inner sep=0pt, anchor=east, outer sep=0pt]
        (box)
        at (frame.south east)
        {\RedBox};
      \draw[framerule]
        ([xshift=-\marginparsep-\marginparwidth]frame.south west) --
        ([xshift=-3pt]box.west);
    \fi
  }
}

\newtheoremstyle{myexa}% name
  {\topsep}%Space above
  {\topsep}%Space below
  {}%Body font
  {}%Indent amount 1
  {\bfseries}% Theorem head font
  {.}%Punctuation after theorem head
  {.5em}%Space after theorem head 2
{}%Theorem head spec

\theoremstyle{myexa}

\newtheorem{exa}{Example}

\usepackage{xparse}
\NewDocumentEnvironment{example}{ooO{12}}
  {\begin{eoexample}\begin{exa}%
    \IfNoValueF{#1}{\marginnote{\flushleft{\color{framerule}{#1}}}[-7.5mm]}}%
  {\IfNoValueF{#2}{\marginnote{\flushleft{\color{framerule}{#2}}}[-#3mm]}%
   \end{exa}\end{eoexample}}

\begin{document}


\lipsum[1-4]

\begin{example}[Simplify $i^{100}$][Answer 1]
This is a worked out example.\\

The solution is typed here, which may go from one page to the next.  The margin can be used to include tables, graphs, or a similar example with answer (but no solution). The question should be right under the line. The answer is above the second line, all in the margin.\\
\lipsum[1]
\end{example}

\lipsum[1]

\begin{example}[Simplify $i^{101}$][Answer: $i$ that is really really
  really long][18]
This is a second example.

Include the solution here.
\lipsum[1]
\end{example}
\end{document}

答案2

您是指这样的吗?这样设置后,第二个注释就可以作为可选参数传递给example。我使用了第二个注释,因为我不清楚您希望用第一个注释做什么。由于该注释位于示例内容的中间,因此其位置可能有所不同。

\documentclass{book}
\usepackage[many]{tcolorbox}
\usepackage{changepage}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{lipsum}
\usepackage{marginnote}

\definecolor{framerule}{RGB}{183,41,41}
\colorlet{tryit}{green!70!black}

\newcommand\RedBox{\textcolor{framerule}{$\square$}}

\newtcolorbox{eoexample}{
  enhanced,
  breakable,
  check odd page,
  toggle left and right,
  boxrule=0pt,
  colback=white,
  arc=0pt,
  outer arc=0pt,
  top=\topsep,
  bottom=\topsep,
  left=0pt,
  right=0pt,
  boxsep=0pt,
  overlay unbroken ={%
    \ifoddpage
    \node[draw,tryit,fill=white]
    (tryit)
    at ([xshift=\marginparsep+0.5\marginparwidth]frame.north east)
    {You Try It!};
    \draw[framerule]
    (frame.north west) -- ([xshift=-3pt]tryit.west);
    \draw[framerule]
    ([xshift=3pt]tryit.east) --
    ([xshift=\marginparsep+\marginparwidth]frame.north east);
    \node[inner sep=0pt, anchor=east]
    (box)
    at ([xshift=\marginparsep+\marginparwidth]frame.south east)
    {\RedBox};
    \draw[framerule]
    (frame.south west) --
    ([xshift=-3pt]box.west);
    \else
    \node[draw,tryit,fill=white]
    (tryit)
    at ([xshift=-\marginparsep-0.5\marginparwidth]frame.north west)
    {You Try It!};
    \draw[framerule]
    (frame.north east) -- ([xshift=3pt]tryit.east);
    \draw[framerule]
    ([xshift=-3pt]tryit.west) --
    ([xshift=-\marginparsep-\marginparwidth]frame.north west);
    \node[inner sep=0pt, anchor=east, outer sep=0pt]
    (box)
    at (frame.south east)
    {\RedBox};
    \draw[framerule]
    ([xshift=-\marginparsep-\marginparwidth]frame.south west) --
    ([xshift=-3pt]box.west);
    \fi
  },
  overlay first={%
    \ifoddpage
    \node[draw,tryit,fill=white]
    (tryit)
    at ([xshift=\marginparsep+0.5\marginparwidth]frame.north east)
    {You Try It!};
    \draw[framerule]
    (frame.north west) -- ([xshift=-3pt]tryit.west);
    \draw[framerule]
    ([xshift=3pt]tryit.east) --
    ([xshift=\marginparsep+\marginparwidth]frame.north east);
    \else
    \node[draw,tryit,fill=white]
    (tryit)
    at ([xshift=-\marginparsep-0.5\marginparwidth]frame.north west)
    {You Try It!};
    \draw[framerule]
    (frame.north east) -- ([xshift=3pt]tryit.east);
    \draw[framerule]
    ([xshift=-3pt]tryit.west) --
    ([xshift=-\marginparsep-\marginparwidth]frame.north west);
    \fi
  },
  overlay last={%
    \ifoddpage
    \node[inner sep=0pt]
    (box)
    at ([xshift=\marginparsep+\marginparwidth]frame.south east)
    {\RedBox};
    \draw[framerule]
    (frame.south west) --
    ([xshift=-3pt]box.west);
    \else
    \node[inner sep=0pt, anchor=east, outer sep=0pt]
    (box)
    at (frame.south east)
    {\RedBox};
    \draw[framerule]
    ([xshift=-\marginparsep-\marginparwidth]frame.south west) --
    ([xshift=-3pt]box.west);
    \fi
  }
}

\newtheoremstyle{myexa}% name
{\topsep}%Space above
{\topsep}%Space below
{}%Body font
{}%Indent amount 1
{\bfseries}% Theorem head font
{.}%Punctuation after theorem head
{.5em}%Space after theorem head 2
{}%Theorem head spec

\theoremstyle{myexa}

\newtheorem{exa}{Example}
\makeatletter
\newenvironment{example}[1][]{%
  \gdef\final@marginnote@example{#1}%
  \begin{eoexample}\begin{exa}%
    }{%
      \def\tempa{}\ifx\tempa\final@marginnote@example\relax\else\marginnote{\flushleft{\color{framerule}{\final@marginnote@example}}}[-1.2cm]\fi
    \end{exa}\end{eoexample}%
}
\makeatother
\begin{document}

  \lipsum[1-4]

  \begin{example}[Answer: 1.]
    This is a worked out example.\\
    \marginnote{\flushleft{\color{framerule}{
          Simplify $i^{100}$.}}}[-.75cm]

    The solution is typed here, which may go from one page to the next.  The margin can be used to include tables, graphs, or a similar example with answer (but no solution). The question should be right under the line. The answer is above the second line, all in the margin.\\
    \lipsum[1]
  \end{example}

  \lipsum[1]

  \begin{example}[Answer: $i$.]
    This is a second example. \\
    \marginnote{\flushleft{\color{framerule}{
          Simplify $i^{101}$.}}}[-.75cm]

    Include the solution here.
    \lipsum[1]
  \end{example}
\end{document}

相关内容