如何在页边距创建两个互相引用的环境?

如何在页边距创建两个互相引用的环境?

我想创建两个环境:一个用于练习,一个用于练习的解决方案。每个练习都应引用其相应解决方案的页面,反之亦然。问题并不完全“对称”,因为解决方案的标签应等于相应练习的标签。到目前为止一切顺利。还有一个要求:我想在页边距中打印引用(练习对解决方案的引用和解决方案对练习的引用)。正如您在下面的 MWE 中看到的,边注不是与练习/解决方案环境垂直对齐的(而是与环境主体的第一行垂直对齐)。我尝试将调用\marginnote{}作为参数合并到练习/解决方案环境中,但未能使其工作。

如何才能做到这一点?

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{amsthm}
\usepackage{marginnote}

\newtheoremstyle{exstyle}%
{0.5em}% space above
{0.5em}% space below
{}% body font
{}% indent amount
{\sffamily\bfseries}% head font
{}% punctuation after head
{\newline}% space after head
{\thmname{#1}\ \thmnumber{#2}\ \thmnote{#3}}% head spec
\theoremstyle{exstyle}% activate style
\newtheorem{exercise}{Exercise}[section]

\newtheoremstyle{solstyle}%
{0.5em}% space above
{0.5em}% space below
{}% body font
{}% indent amount
{\sffamily\bfseries}% head font
{}% punctuation after head
{\newline}% space after head
{\thmname{#1}\ \ref{#2}\ \thmnote{#3}}% head spec; obviously, the `\ref{#2}` fails...
\theoremstyle{solstyle}% activate style
\newtheorem{solution}{Solution}

\begin{document}
\begin{exercise}[(Exercise title)]\label{ex}\marginnote{Sol.\ on p.~\pageref{sol}}
  This is the exercise
\end{exercise}
\clearpage
\begin{solution}[(Solution title)]\label{sol}\marginnote{Ex.\ on p.~\pageref{ex}}
  This is the solution
\end{solution}
\end{document}

更新

我尝试过的一种方法是调整证明环境以适应解决方案;见下文。这会将边注放在正确的垂直水平,但需要手动引入换行符(在解决方案标题之后)。不确定是否有更优雅的方法……

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{marginnote}

% Exercise
\newtheoremstyle{exstyle}%
{0.5em}% space above
{0.5em}% space below
{}% body font
{}% indent amount
{\sffamily\bfseries}% head font
{}% punctuation after head
{\newline}% space after head
{\thmname{#1}\ \thmnumber{#2}\ \thmnote{#3}}% head spec
\theoremstyle{exstyle}% activate style
\newtheorem{exercise}{Exercise}[section]

% Solution (adapted from proof environment)
\makeatletter
\newenvironment{solution}[2][]{\par
  \pushQED{\qed}%
  \normalfont\topsep6\p@\@plus6\p@\relax
  \trivlist
\item[\hskip\labelsep\sffamily\bfseries Solution~\ref{#2}\ #1]%
    \marginnote{Ex.\ on p.~\pageref{#2}}\ignorespaces%
}{%
  \popQED\endtrivlist\@endpefalse
}
\makeatother

\begin{document}
\begin{exercise}[(Exercise title)]\label{ex1}\marginnote{Sol.\ on p.~\pageref{sol1}}
  This is the exercise
\end{exercise}
\begin{solution}[(Solution title)]{ex1}\label{sol1}\\
  This is the solution
\end{solution}

\begin{exercise}[(Exercise title)]\label{ex2}\marginnote{Sol.\ on p.~\pageref{sol2}}
  \vspace{-\baselineskip}
  \begin{enumerate}
  \item Item 1
  \end{enumerate}
\end{exercise}
\begin{solution}[(Solution title)]{ex2}\label{sol2}
  \begin{enumerate}
  \item Item 1
  \end{enumerate}
\end{solution}
\end{document}

更新2

这显示了我想要实现的目标(现在有了两个“类似证明”的环境)。正如 David 指出的那样这里这通常不是我们想要的(因为我们离开了垂直模式)

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{amsmath}
\usepackage{mathtools}
\usepackage{amssymb}
\usepackage{amsthm}
\usepackage{etoolbox}
\usepackage{enumitem}
\usepackage{marginnote}

\setlist{
  align=left,
  labelsep=*,
  leftmargin=*,
  topsep=1mm,
  itemsep=0mm
}

% Exercise
% Note: One has to provide \\ by hand in order to get the linebreak after the
%       exercise head (unless itemize/enumerate follows)
\newcounter{counter}% exercise counter
\makeatletter
\newenvironment{exercise}[2][]{\refstepcounter{counter}\par
  \normalfont\topsep6\p@\@plus6\p@\relax
  \trivlist
\item[\hskip\labelsep\sffamily\bfseries Exercise~\thecounter\ #1]%
    \marginnote{Sol.\ on p.~\pageref{#2}}\ignorespaces
}{%
  \endtrivlist\@endpefalse
}
\makeatother

% Solution
% Note: One has to provide \\ by hand in order to get the linebreak after the
%       exercise head (unless itemize/enumerate follows)
\makeatletter
\newenvironment{solution}[2][]{\par
  \pushQED{\qed}%
  \normalfont\topsep6\p@\@plus6\p@\relax
  \trivlist
\item[\hskip\labelsep\sffamily\bfseries Solution~\ref{#2}\ #1]%
    \marginnote{Ex.\ on p.~\pageref{#2}}\ignorespaces
    \ignorespaces%
}{%
  \popQED\endtrivlist\@endpefalse
}
\makeatother

\begin{document}
\begin{exercise}[(Exercise title)]{sol1}\label{ex1}\\
  This is the exercise
\end{exercise}
\begin{solution}[(Solution title)]{ex1}\label{sol1}\\
  This is the solution
\end{solution}

\begin{exercise}[(Exercise title)]{sol2}\label{ex2}
  \begin{enumerate}
  \item Item 1
  \end{enumerate}
\end{exercise}
\begin{solution}[(Solution title)]{ex2}\label{sol2}
  \begin{enumerate}
  \item Item 1
  \end{enumerate}
\end{solution}
\end{document}

答案1

仅有相互参考的部分解决方案:您需要自动生成的标签。

在我看来,练习/解决方案环境应该被包装到其他环境中,进行一些设置,尤其是对齐\marginnote

\documentclass{scrartcl}

\usepackage[T1]{fontenc}
\usepackage[american]{babel}
\usepackage{amsthm}
\usepackage{marginnote}

\newtheoremstyle{exstyle}%
{0.5em}% space above
{0.5em}% space below
{}% body font
{}% indent amount
{\sffamily\bfseries}% head font
{}% punctuation after head
{\newline}% space after head
{\thmname{#1}\ \thmnumber{#2}\ \thmnote{#3}\label{ex:\number\value{exercise}}}% head spec
\theoremstyle{exstyle}% activate style
\newtheorem{exercise}{Exercise}[section]

\newtheoremstyle{solstyle}%
{0.5em}% space above
{0.5em}% space below
{}% body font
{}% indent amount
{\sffamily\bfseries}% head font
{}% punctuation after head
{\newline}% space after head
{\thmname{#1}\ \ref{ex:\number\value{exercise}}\ \thmnote{#3}\label{sol:\number\value{exercise}}}% head spec; obviously, the `\ref{#2}` fails...
\theoremstyle{solstyle}% activate style
\newtheorem{solution}{Solution}

\begin{document}
\begin{exercise}[(Exercise title)]\label{ex}\marginnote{Sol.\ on p.~\pageref{sol}}
  This is the exercise
\end{exercise}
\clearpage
\begin{solution}[(Solution title)]\label{sol}\marginnote{Ex.\ on p.~\pageref{ex}}
  This is the solution
\end{solution}


\begin{exercise}[(Foo question)]\marginnote{Sol.\ on p.~\pageref{sol:\number\value{exercise}}}
  This is the exercise
\end{exercise}
\clearpage
\begin{solution}[(foo solution)]\marginnote{Ex.\ on p.~\pageref{ex:\number\value{exercise}}}
  This is the solution
\end{solution}

\end{document}

编辑包装命令模仿定理并提供正确的链接/标签等。

\documentclass{scrbook}

\usepackage{marginnote}
\usepackage{xparse}

\usepackage{hyperref}

\newcounter{exercise}
\newcounter{solution}


\newwrite\allmysolutions

\AtBeginDocument{%
  \immediate\openout\allmysolutions=\jobname.sol
}

\newcommand{\CloseSolutions}{%
  \immediate\closeout\allmysolutions
}

\newcommand{\InputSolutions}{%
  \InputIfFileExists{\jobname.sol}{}{}%
}


\NewDocumentCommand{\DoExercise}{O{}+m+m+o}{%
  \newpage% Just for demo of pages etc. 
  \refstepcounter{exercise}\label{ex:\number\value{exercise}}
  Exercise (#2) \theexercise \IfValueTF{#4}{\marginnote{Sol.\ on p.~\pageref{sol:\number\value{exercise}}}}{\refstepcounter{solution}}

  #3
  \IfValueT{#4}{%
    \immediate\write\allmysolutions{%
      \string\newpage% Just for demo
      \string\refstepcounter{solution}\string\label{sol:\number\value{exercise}}^^J%
      Solution (Solution of #2) \string\thesolution \unexpanded{\marginnote{Exercise.\ on p.~\pageref{ex:\number\value{exercise}}}}

      \unexpanded{#4}
      \vskip\baselineskip
    }
  }
  \vskip\baselineskip
}


\begin{document}

\DoExercise{Einstein}{$E = mc^2$}[Yes, it's true]


\DoExercise{Heisenberg}{Proof $\Delta x\Delta p \ge \hbar$}[Yes, it's true -- it's science]

\CloseSolutions
\InputSolutions

\end{document}

相关内容