自动切换边距

自动切换边距

在此处输入图片描述

我创建了一些新命令,用于在“你尝试一下”下的边缘输入问题和答案:

\newcommand{\margins}[1]{ \marginnote{
\flushleft{\color{framerule}{Simplify: \;  #1}}}[-1cm] }

\newcommand{\margina}[1]{ \marginnote{
\flushleft{\color{framerule}{Answer:  \; #1}}}[-1cm] }

有没有更好的方法使用你的代码来实现它?

答案1

这是一个使用选项tcolorbox及其check odd page功能toggle left and right;我还使用了amsthm包定义定理样式和练习环境;example然后该环境允许您使用所需的格式排版练习:

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

\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={%
    \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]
        (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]
        (box)
        at ([xshift=-\marginparsep-\marginparwidth]frame.south west)
        {\RedBox};  
      \draw[framerule] 
        (frame.south east) -- 
        ([xshift=3pt]box.east);
    \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}

\begin{example}
\lipsum[1]
\end{example}
\clearpage
\begin{example}
\lipsum[1]
\end{example}

\end{document}

结果:

在此处输入图片描述

奇数页布局图片:

在此处输入图片描述

偶数页布局的图像:

在此处输入图片描述

答案2

您可以使用此代码获取保证金

\documentclass[a4paper,11pt]{book}
\usepackage{tikz}
\newenvironment{lrmargin}{%
\begin{list}{}{%
\setlength{\topsep}{0pt}%
\ifodd\value{page}%
\setlength{\leftmargin}{0pt}%
\setlength{\rightmargin}{-.2\textwidth}%
\else%
\setlength{\leftmargin}{-.2\textwidth}%
\setlength{\rightmargin}{0pt}%
\fi%
\setlength{\listparindent}{\parindent}%
\setlength{\itemindent}{\parindent}%
\setlength{\parsep}{\parskip}%
}%
\item[]}{\end{list}}
\begin{document}
test \hrulefill test\\
test \hrulefill test
\begin{lrmargin}
test \hrulefill test
\end{lrmargin}
test \hrulefill test
\newpage
test \hrulefill test\\
test \hrulefill test
\begin{lrmargin}
test \hrulefill test
\end{lrmargin}
test \hrulefill test
\end{document}

相关内容