我想要实现的目标如下:
当然,如果可以做到这一点,那么在偶数页和奇数页之间可以切换。目前,我只对不出现在 LHS 边距上感兴趣。我目前没有 MWE,但我抛出了这个问题。也许你可以从这里开始:
\documentclass[letterpaper]{article}
\usepackage[dvipsnames]{xcolor}
\usepackage{marginnote}
\usepackage{etoolbox}
\usepackage{calc}
\makeatletter
\patchcmd{\@mn@margintest}{\@tempswafalse}{\@tempswatrue}{}{}
\patchcmd{\@mn@margintest}{\@tempswafalse}{\@tempswatrue}{}{}
\reversemarginpar
\makeatother
\setlength{\marginparwidth}{2in}
\oddsidemargin 2in
\def\fbx#1{\vbox{\hbox{\hbox{#1}\setbox0\lastbox\copy0\kern\fboxsep\vrule width\fboxrule depth\dimexpr \fboxsep+\dp0\relax}%
\hrule height\fboxrule}}
\newcommand\caution[1]{%
\renewcommand*{\raggedleftmarginnote}{}
\relax\leavevmode\marginnote{%
{\sffamily\bfseries\textcolor{BrickRed}{Caution!}}\\[1pt]%
\fbx{\parbox[t]{\dimexpr\linewidth-3\marginparsep\relax}{#1}}%
}
}
\begin{document}
test \caution{some random text for testing caution frame}. some random text some random text some random text some random text some random text.
\end{document}
答案1
以下是使用该mdframed
包和\marginnote
从该 marginnote
包中获取信息的一种可能性:
\documentclass{article}
\usepackage[lmargin=5cm,textwidth=15cm,marginparwidth=4cm]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{ragged2e}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{marginnote}
\usetikzlibrary{calc}
\usepackage{lipsum}
\reversemarginpar
\newsavebox\mybox
\newlength\BoxHt
\newcommand\caution[2][-2.2\baselineskip]{%
\begin{lrbox}{\mybox}
\parbox{\marginparwidth}{#2}
\end{lrbox}%
\settoheight\BoxHt{\usebox\mybox}%
\raisebox{\BoxHt}[0pt][0pt]{\marginnote{%
\begin{mdframed}[
userdefinedwidth=\marginparwidth,
innerleftmargin=3pt,
innerrightmargin=3pt,
linecolor=BrickRed,
frametitle=\colorbox{white}{\space Caution\space},
frametitlefont=\color{BrickRed}\sffamily,
innertopmargin=10pt,
frametitleaboveskip=-\ht\strutbox,
frametitlebelowskip=-\ht\strutbox,
frametitlealignment=\raggedright,
singleextra={\fill[BrickRed] let \p1=(P), \p2=(O) in
( $ (P|-0,0.5*\y2+0.5*\y1) + (0,-4pt) $ ) -- +(4pt,4pt) -- +(0,8pt) -- cycle;}
]\RaggedRight\small#2\end{mdframed}}[#1]}}
\begin{document}
\lipsum*[3-4]\caution{\lipsum[2]}\lipsum[3-5]
Some test text\caution{Some text goes here just to illustrate the command}
\end{document}
放大的图像:
可选参数允许\caution
您在必要时控制垂直移动。
下面是另一个更简单的选项(我个人更喜欢),使用tikz
(和tikzpagenodes
):
\documentclass{article}
\usepackage[lmargin=5cm,textwidth=15cm,marginparwidth=4cm]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{ragged2e}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{lipsum}
\newcounter{mycaution}
\newcommand\tikzmark[1]{%
\tikz[remember picture,overlay]\node[inner xsep=0pt,outer sep=0pt] (#1) {};}
\newcommand\caution[1]{%
\stepcounter{mycaution}%
\tikzmark{\themycaution}%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=BrickRed,anchor=east,xshift=-\marginparsep]
(mybox\themycaution)
at ([yshift=3pt]current page text area.west|-\themycaution)
{\parbox{\marginparwidth}{\vskip10pt\RaggedRight\small#1}};
\node[fill=white,font=\color{BrickRed}\sffamily,anchor=west,xshift=7pt]
at (mybox\themycaution.north west) {\ Caution!\ };
\fill[BrickRed]
([yshift=3pt]mybox\themycaution.east) --
([xshift=3pt]mybox\themycaution.east) --
([yshift=-3pt]mybox\themycaution.east) -- cycle;
\end{tikzpicture}%
}
\begin{document}
\lipsum*[3-4]\caution{\lipsum[2]}\lipsum[3-5]
Some test text\caution{Some text goes here just to illustrate the command}
\end{document}
放大后的图像:
改进的版本;现在有一个可选参数允许您更改框和指针的位置;可能的值是b
、t
和c
,分别将指针放在框的底部、顶部和中心:
\documentclass{article}
\usepackage[lmargin=5cm,textwidth=15cm,marginparwidth=4cm]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{ragged2e}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{lipsum}
\newcounter{mycaution}
\newcommand\pointeranchor{}
\newcommand\boxanchor{}
\newlength\boxvshift
\newlength\uppertrianglecorner
\newcommand\tikzmark[1]{%
\tikz[remember picture,overlay]\node[inner xsep=0pt,outer sep=0pt] (#1) {};}
\newcommand\caution[2][c]{%
\stepcounter{mycaution}%
\tikzmark{\themycaution}%
\if#1b\relax
\renewcommand\pointeranchor{mybox\themycaution.south east}%
\renewcommand\boxanchor{south east}%
\setlength\boxvshift{-10pt}%
\setlength\uppertrianglecorner{13pt}%
\else
\if#1t\relax
\renewcommand\pointeranchor{mybox\themycaution.north east}%
\renewcommand\boxanchor{north east}%
\setlength\boxvshift{10pt}%
\setlength\uppertrianglecorner{-7pt}%
\else
\if#1c\relax
\renewcommand\pointeranchor{mybox\themycaution.east}%
\renewcommand\boxanchor{east}%
\setlength\boxvshift{0pt}%
\setlength\uppertrianglecorner{3pt}%
\fi\fi\fi%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=BrickRed,anchor=\boxanchor,xshift=-\marginparsep,yshift=\boxvshift]
(mybox\themycaution)
at ([yshift=3pt]current page text area.west|-\themycaution)
{\parbox{\marginparwidth}{\vskip10pt\RaggedRight\small#2}};
\node[fill=white,font=\color{BrickRed}\sffamily,anchor=west,xshift=7pt]
at (mybox\themycaution.north west) {\ Caution!\ };
\fill[BrickRed]
([yshift=\uppertrianglecorner]\pointeranchor) --
([yshift=\uppertrianglecorner-3pt,xshift=3pt]\pointeranchor) --
([yshift=\uppertrianglecorner-6pt]\pointeranchor) -- cycle;
\end{tikzpicture}%
}
\newcommand\Test{Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum felis. Donec nonummy pellentesque ante. Phasellus adipiscing
semper elit.}
\begin{document}
\lipsum*[4]\caution[t]{\Test}\lipsum[3]\par\bigskip
\lipsum*[4]\caution{\Test}\lipsum[3]\par\bigskip
\lipsum*[4]\caution[b]{\Test}\lipsum[3]
\end{document}
t
如果框出现在文本区域的第一行,则该选项很有用;b
如果框出现在文本区域底部附近,则可以使用该选项。
注释需要稍作变化;现在 \caution 有三个可选参数和一个强制参数:
\caution[<pos>][<color>][<title>]{<text>}
其中 ias 是代码中紧接着之前的一个,可以是b
、t
或c
(默认值= c
);<color>
控制用于框架和标题的颜色(默认值= BrickRed
);<title>
更改用于框架的标题(默认值= Caption!
);<text>
是注释的内容。
\documentclass{article}
\usepackage[lmargin=5cm,textwidth=15cm,marginparwidth=4cm]{geometry}
\usepackage[dvipsnames]{xcolor}
\usepackage{ragged2e}
\usepackage{xparse}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usetikzlibrary{calc}
\usepackage{lipsum}
\newcounter{mycaution}
\newcommand\pointeranchor{}
\newcommand\boxanchor{}
\newlength\boxvshift
\newlength\uppertrianglecorner
\newcommand\tikzmark[1]{%
\tikz[remember picture,overlay]\node[inner xsep=0pt,outer sep=0pt] (#1) {};}
\NewDocumentCommand{\caution}{O{c}O{BrickRed}O{Caution!}m}{%
\stepcounter{mycaution}%
\tikzmark{\themycaution}%
\if#1b\relax
\renewcommand\pointeranchor{mybox\themycaution.south east}%
\renewcommand\boxanchor{south east}%
\setlength\boxvshift{-10pt}%
\setlength\uppertrianglecorner{13pt}%
\else
\if#1t\relax
\renewcommand\pointeranchor{mybox\themycaution.north east}%
\renewcommand\boxanchor{north east}%
\setlength\boxvshift{10pt}%
\setlength\uppertrianglecorner{-7pt}%
\else
\if#1c\relax
\renewcommand\pointeranchor{mybox\themycaution.east}%
\renewcommand\boxanchor{east}%
\setlength\boxvshift{0pt}%
\setlength\uppertrianglecorner{3pt}%
\fi\fi\fi%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=#2,anchor=\boxanchor,xshift=-\marginparsep,yshift=\boxvshift]
(mybox\themycaution)
at ([yshift=3pt]current page text area.west|-\themycaution)
{\parbox{\marginparwidth}{\vskip10pt\RaggedRight\small#4}};
\node[fill=white,font=\color{#2}\sffamily,anchor=west,xshift=7pt]
at (mybox\themycaution.north west) {\ #3\ };
\fill[#2]
([yshift=\uppertrianglecorner]\pointeranchor) --
([yshift=\uppertrianglecorner-3pt,xshift=3pt]\pointeranchor) --
([yshift=\uppertrianglecorner-6pt]\pointeranchor) -- cycle;
\end{tikzpicture}%
}
\newcommand\Test{Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum felis. Donec nonummy pellentesque ante. Phasellus adipiscing
semper elit.}
\begin{document}
\lipsum*[4]\caution[t]{\Test}\lipsum[3]\par\bigskip
\lipsum*[4]\caution[c][cyan!80!black]{\Test}\lipsum[3]\par\bigskip
\lipsum*[4]\caution[b][orange][Defcon 5!]{\Test}\lipsum[3]
\end{document}
答案2
我尝试改进 Gonzalo Medina 的代码以便能够使用双面文档。
这四个参数(前三个是可选的,最后一个是必需的)与 Gonzalo Medina 的代码相同。
我已尽力使指针面向之前的文本\caution
(这就是我的 MWE 如此之长的原因,我不得不删除\lipsum
并粘贴整个文本以将 放在\caution
中间)。这样,选择b
、t
或c
作为第一个参数不仅决定了指针在框内的位置,还决定了框相对于命令周围文本的位置\caution
。
此外,我建议使用\caution
内部\marginnote
以避免分割段落\caution
。
book
我尝试用和类来编写此代码tufte-book
,似乎运行良好。但是,使用ifoddpage
需要用 pdfLaTeX 进行两到三次编译
\documentclass[twoside,10pt]{book}
\usepackage[dvipsnames]{xcolor}
\usepackage{ragged2e}
\usepackage{xparse}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{tikzpagenodes}
\usepackage{lipsum}
\usepackage{ifoddpage}
\usepackage[fulladjust]{marginnote}
\newcounter{mycaution}
\newlength\trianglexshift
\newlength\boxhshift
\newlength\boxvshift
\newlength\noteheight
\newlength\uppertrianglecorner
\newcommand\side{}
\newcommand\boxanchor{}
\newcommand\otherside{}
\newcommand\pointeranchor{}
\newcommand\tikzmark[1]{%
\tikz[remember picture,overlay]\node[inner xsep=0pt,outer sep=0pt] (#1) {};}
\NewDocumentCommand{\caution}{O{c}O{BrickRed}O{Caution!}m}{%
%
\checkoddpage
\ifoddpageoroneside
\setlength\trianglexshift{-3pt}
\setlength\boxhshift{\marginparsep}
\renewcommand{\side}{west}
\renewcommand{\otherside}{east}
\else
\setlength\trianglexshift{3pt}
\setlength\boxhshift{-\marginparsep}
\renewcommand{\side}{east}%
\renewcommand{\otherside}{west}
\fi
\settoheight{\noteheight}{\parbox{\marginparwidth}{\RaggedRight\small#4}}
\stepcounter{mycaution}%
\tikzmark{\themycaution}%
\if#1b\relax
\renewcommand\pointeranchor{mybox\themycaution.south \side}%
\renewcommand\boxanchor{south \side}%
\setlength\boxvshift{\noteheight}%
\addtolength\boxvshift{0.7\baselineskip}
\setlength\uppertrianglecorner{13pt}%
\else
\if#1t\relax
\renewcommand\pointeranchor{mybox\themycaution.north \side}%
\renewcommand\boxanchor{north \side}%
\setlength\boxvshift{-\noteheight}%
\addtolength\boxvshift{1.3\baselineskip}
\setlength\uppertrianglecorner{-7pt}%
\else
\if#1c\relax
\renewcommand\pointeranchor{mybox\themycaution.\side}%
\renewcommand\boxanchor{\side}%
\setlength\boxvshift{\baselineskip}%
\setlength\uppertrianglecorner{3pt}%
\fi\fi\fi%
\begin{tikzpicture}[remember picture,overlay]
\node[draw=#2,anchor=\side,xshift=\boxhshift,yshift=\boxvshift]
(mybox\themycaution)
at ([yshift=3pt]current page text area.\otherside|-\themycaution)
{\parbox{\marginparwidth}{\vskip10pt\RaggedRight\small#4}};
\node[fill=white,font=\color{#2}\sffamily,anchor=west,xshift=7pt]
at (mybox\themycaution.north west) {\ #3\ };
\fill[#2]
([yshift=\uppertrianglecorner]\pointeranchor) --
([yshift=\uppertrianglecorner-3pt,xshift=\trianglexshift]\pointeranchor) --
([yshift=\uppertrianglecorner-6pt]\pointeranchor) -- cycle;
\end{tikzpicture}}
\newcommand\Test{Nulla malesuada porttitor diam. Donec felis erat, congue non, volutpat at, tincidunt tristique, libero. Vivamus viverra fermentum felis. Donec nonummy pellentesque ante. Phasellus adipiscing
semper elit.}
\begin{document}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque\marginnote{\caution[t][cyan!80!black]{\Test}}. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada\marginnote{\caution{\Test}} eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
Quisque ullamcorper placerat ipsum. Cras nibh. Morbi vel justo vitae lacus
tincidunt ultrices. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In
hac habitasse platea dictumst. Integer tempus convallis augue. Etiam facilisis.
Nunc elementum fermentum wisi. Aenean placerat. Ut imperdiet, enim sed
gravida sollicitudin, felis odio placerat quam, ac pulvinar elit purus eget enim.
Nunc vitae tortor. Proin tempus nibh sit amet nisl. Vivamus quis tortor vitae
risus porta vehicula.
\lipsum[3]\par\bigskip
\newpage
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit,
vestibulum ut, placerat ac, adipiscing vitae, felis. Curabitur dictum gravida
mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus
et netus et malesuada fames ac turpis egestas. Mauris ut leo. Cras viverra
metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices. Phasellus
eu tellus sit amet tortor gravida placerat. Integer sapien est, iaculis in, pretium
quis, viverra ac, nunc. Praesent eget sem vel leo ultrices bibendum. Aenean
faucibus. Morbi dolor nulla, malesuada eu, pulvinar at, mollis ac, nulla. Cur-
abitur auctor semper nulla\marginnote{\caution[b][ForestGreen][Nice !]{\Test}}. Donec varius orci eget risus. Duis nibh mi, congue
eu, accumsan eleifend, sagittis quis, diam. Duis eget orci sit amet orci dignissim
rutrum.
Quisque ullamcorper placerat ipsum. Cras nibh. Morbi vel justo vitae lacus
tincidunt ultrices. Lorem ipsum dolor sit amet, consectetuer adipiscing elit. In
hac habitasse platea dictumst. Integer tempus convallis augue. Etiam facilisis.
Nunc elementum fermentum wisi. Aenean placerat. Ut imperdiet, enim sed
gravida sollicitudin, felis odio placerat quam, ac pulvinar elit purus eget enim.
Nunc vitae tortor. Proin tempus nibh sit amet nisl. Vivamus quis tortor vitae
risus porta vehicula.
\end{document}
奇怪的一页
偶数页
答案3
修订版包括两个边距!第一个强制参数是l
或 ,r
表示注释应放在哪个边距中。第二个参数是注释。修订版允许使用可选参数,该参数指定框相对于不变箭头指针的向上移动。
在下面显示的示例中,我将左侧框相对于默认位置向上移动 2ex。在右侧框中,我将其保留在默认位置。
\topinset
通过用等效宏替换弃用的宏,编辑以升级语法\stackinset
。
\documentclass{article}
\usepackage{xcolor}
\usepackage{stackengine}
\usepackage{lipsum}
\usepackage{MNsymbol}
\usepackage{graphicx}
\newcommand*\caution[3][0pt]{%
\if l#2\reversemarginpar\def\pointer{\filledmedtriangleright}%
\def\stackalignment{r}\fi%
\if r#2\normalmarginpar\def\pointer{\filledmedtriangleleft}%
\def\stackalignment{l}\fi%
\marginpar{%
\stackinset{\stackalignment}{-2ex}{t}{3ex+#1}{%
\scalebox{1.5}{\textcolor{yellow}{$\pointer$}}}{%
\belowbaseline[-1.5\baselineskip-#1]{%
\stackengine%
{-5pt}%
{\fcolorbox{yellow}{white}{\parbox{1.8cm}%
{\vspace{3pt}\raggedright#3}}}%
{~\colorbox{white}{\sffamily Caution}}%
{O}%
{l}%
{F}%
{F}%
{S}%
}%
}%
}%
}
\begin{document}
\lipsum[1-2]
this is a\caution[2ex]{l}{some text for the caution box} test
\lipsum[3]
This is another test\caution{r}{another caution box test testing the
right side}
\lipsum[4]
\end{document}
这是特写: