如何在 mdframe 旁边的页边距中放置图形?

如何在 mdframe 旁边的页边距中放置图形?

在我的文本中,我想在页边空白处使用一些图标来标记元素类型,例如示例或定理。下图说明了这一点:

在此处输入图片描述

我的文本是双面的,所以我想将图标自动放置在“正确”的一侧,也就是外侧。我希望彩色框在灰色框旁边垂直居中对齐,并在页边距水平居中对齐。对于该框,我将使用 mdframed 包。实现此目的的方法是什么?

这是我的 MWE,我想扩展它的environment定义:

\documentclass[12pt,twoside,openright,a4paper,oldfontcommands]{memoir}

\usepackage{mdframed}
\usepackage[table]{xcolor}

\definecolor{exampleBorder}{RGB}{235,235,235}
\newenvironment{example}{%
    \mdfsetup{frametitlealignment=\raggedright}
    \begin{mdframed}[
        outerlinewidth=0,
        skipbelow=5pt,
        skipabove=5pt,
        innertopmargin=8pt,
        innerbottommargin=8pt,
        innerleftmargin=8pt,
        innerrightmargin=8pt,
        linecolor=exampleBorder]%
}{%
    \end{mdframed}%
}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum, 
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
This is an example. This is an example. This is an example. This is an example.
This is an example. 
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum, 
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\end{document}

谢谢你!

答案1

您可以使用marginnote

\begin{example}
    \marginnote{\includegraphics[width=0.5\marginparwidth]{example-image-a}} ........

代码:

\documentclass[12pt,twoside,openright,a4paper,oldfontcommands]{memoir}

\usepackage{mdframed}
\usepackage[table]{xcolor}

\definecolor{exampleBorder}{RGB}{235,235,235}
\newenvironment{example}{%
    \mdfsetup{frametitlealignment=\raggedright}
    \begin{mdframed}[
        outerlinewidth=0,
        skipbelow=5pt,
        skipabove=5pt,
        innertopmargin=8pt,
        innerbottommargin=8pt,
        innerleftmargin=8pt,
        innerrightmargin=8pt,
        linecolor=exampleBorder]%
}{%
    \end{mdframed}%
}
\usepackage{marginnote,graphicx}
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
\marginnote{\includegraphics[width=0.5\marginparwidth]{example-image-a}}
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\clearpage

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
\marginnote{\includegraphics[width=0.5\marginparwidth]{example-image-b}}
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\end{document}

在此处输入图片描述


如果您正在使用memoir,那么您可以使用sideparmarginpar提供的,而memoir无需任何额外的包。但请记住\marginpar浮动和\sidepar不浮动。

\documentclass[12pt,twoside,openright,a4paper,oldfontcommands]{memoir}

\usepackage{mdframed}
\usepackage[table]{xcolor}

\definecolor{exampleBorder}{RGB}{235,235,235}
\newenvironment{example}{%
    \mdfsetup{frametitlealignment=\raggedright}
    \begin{mdframed}[
        outerlinewidth=0,
        skipbelow=5pt,
        skipabove=5pt,
        innertopmargin=8pt,
        innerbottommargin=8pt,
        innerleftmargin=8pt,
        innerrightmargin=8pt,
        linecolor=exampleBorder]%
}{%
    \end{mdframed}%
}
\usepackage{graphicx}
\setmarginnotes{5mm}{25mm}{0.5\baselineskip} %% <---- adjust these.
\sideparmargin{outer} 
\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
This is an example. This is an example. This is an example. This is an example.
\sidepar{\includegraphics[width=0.5\marginparwidth]{example-image-a}}
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\clearpage

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{example}
This is an example. This is an example. This is an example. This is an example.
\sidepar{\includegraphics[width=0.5\marginparwidth]{example-image-b}}
This is an example.
\end{example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\end{document}

在此处输入图片描述


这是使用 的另一种方法tikz。在这里,您可以摆脱mdframed并使用 执行所有操作,tikz但由于我不知道您的用例,因此我保持mdframed原样。这适用于任何文档类。

\documentclass[12pt,twoside,openright,a4paper,oldfontcommands,table
]{memoir}

\usepackage{mdframed,tikz}

\definecolor{exampleBorder}{RGB}{235,235,235}
\mdfdefinestyle{exampl}{
        outerlinewidth=0,
        skipbelow=5pt,
        skipabove=5pt,
        innertopmargin=8pt,
        innerbottommargin=8pt,
        innerleftmargin=8pt,
        innerrightmargin=8pt,
        linecolor=exampleBorder,
}
\mdfsetup{frametitlealignment=\raggedright}

\surroundwithmdframed[style=exampl]{Example}
\usepackage{environ}
\usepackage{ifoddpage}

\newcommand{\Graphics}[1]{\includegraphics[width=0.5\marginparwidth]{#1}}
\NewEnviron{Example}[1][]{%
  \ignorespacesafterend
  \checkoddpage
  \ifoddpage
    \noindent 
    \begin{tikzpicture}
      \node[inner sep=0pt,outer sep=0pt,text width=\linewidth,align=justify] (a) {\BODY};
      \node[anchor=west,inner sep=0pt,outer sep=0pt,overlay] at ([xshift=2em]a.east)
           {#1};
    \end{tikzpicture}% 
    \else
    \noindent
    \begin{tikzpicture}%
      \node[inner sep=0pt,outer sep=0pt,text width=\linewidth,align=justify] (a) {\BODY};
      \node[anchor=east,inner sep=0pt,outer sep=0pt,overlay] at ([xshift=-2em]a.west)
          {#1};
    \end{tikzpicture}% 
    \fi     
}%

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{Example}[\Graphics{example-image-a}]
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{Example}%

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\begin{Example}
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{Example}%

\clearpage

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio

\begin{Example}[\Graphics{example-image-b}]
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
This is an example. This is an example. This is an example. This is an example.
This is an example.
\end{Example}

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nam gravida dolor vestibulum,
semper eros nec, accumsan quam. Fusce in auctor erat, eget interdum magna. Duis semper odio
\end{document}

在此处输入图片描述

相关内容