如何使用 mdframed 在框架线旁边添加数字

如何使用 mdframed 在框架线旁边添加数字

我刚刚看到这个mdframed包,觉得它非常有用。但是,我想在框线旁边添加数字,就像我附上的图片一样(看红色数字,这就是我想要的)。有办法吗?

这是我用来创建丘吉尔名言的代码:

\documentclass[11pt,twoside,openright]{book}
\usepackage{mdframed}

\begin{document}

\begin{mdframed}[linewidth=2,linecolor=black, innertopmargin=-.1em, innerbottommargin=0em, topline=false, rightline=false,bottomline=false, linenumber=1]
    
      \textit{A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.}
      \end{mdframed}
      \begin{center}
        {\textbf{\textit{--- Winston Churchill}}}
      \end{center}

\end{document}

这就是我想要的样子

答案1

我不使用mdframed,我提出一个可能的想法tikz

  • 首次采用无mdframed
  • 一秒钟就mdframed放进一个盒子里,然后放置数字。

毫无疑问,还有很多更好的事情要做。

\documentclass{article}
%https://tex.stackexchange.com/questions/670763/how-to-add-numbers-next-to-the-frame-line-using-mdframed
\usepackage{showframe}
\usepackage{tikz}
\usepackage{lipsum}%<--- comment in the final doc
\usepackage{mdframed}%<--- comment in the final doc
\ExplSyntaxOn
\NewDocumentCommand{\myquote}{m m m}{
    \noindent
%%%%%%%%%%%%%%%%%%%%%
    \if_case:w \int_mod:nn {#1} {2}%<--- test #1 even or odd
        \hfill
        \begin{tikzpicture}[outer~sep=4pt]
            %\node [draw] (T) {%<-- uncomment to see the box and comment the next line
            \node  (T) {%
                \begin{minipage}{0.8\linewidth}%
                    \textit{#2}
                \end{minipage}};
            \node at (T.south~east)[red,anchor=south~west]{\Large\bfseries #1};
            \draw [line~width=2pt](T.north~east) -- (T.south~east);
            \node at (T.south)[below]{%
                \begin{minipage}{0.8\linewidth}%
                    \begin{center}
                        \textbf{\textit{---~#3}}
                    \end{center}
                \end{minipage}};
        \end{tikzpicture}
    \else: 
        \begin{tikzpicture}[outer~sep=4pt]
            %\node [draw] (T) {%
            \node  (T) {%
                \begin{minipage}{0.8\linewidth}%
                    \textit{#2}
                \end{minipage}};
            \node at (T.south~west)[red,anchor=south~east]{\Large\bfseries #1};
            \draw [line~width=2pt](T.north~west) -- (T.south~west);
            \node at (T.south)[below]{%
                \begin{minipage}{0.8\linewidth}%
                    \begin{center}
                        \textbf{\textit{---~#3}}
                    \end{center}
                \end{minipage}};
        \end{tikzpicture}
        \hfill\null 
    \fi:
}
\ExplSyntaxOff
\begin{document}
{\Large Without \texttt{mdframed}}

\bigskip
\myquote{1}{A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.}{Winston Churchill}

\myquote{2}{Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did. So throw off the bowlines, sail away from the safe harbor. Catch the trade winds in your sails.}{Marc Twain}

\myquote{3}{\lipsum[1]}{Nobody}

{\Large With \texttt{mdframed}}

\bigskip
\noindent
\begin{tikzpicture}
    \node at (0,0)(T){\begin{minipage}{0.8\linewidth}
\begin{mdframed}[linewidth=2,linecolor=black, innertopmargin=-.1em, innerbottommargin=0em, topline=false, rightline=false,bottomline=false]
\par  
    \textit{A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.}
    \end{mdframed}
\end{minipage}};
\node at (T.south west)[red,anchor=south east]{\Large\bfseries 1};
\end{tikzpicture}
    \begin{center}
    {\textbf{\textit{--- Winston Churchill}}}
\end{center}
\end{document}

代码的小分解(由 projetmbc 提出)

\ExplSyntaxOn

\tl_new:N \quoteleftfill
\tl_new:N \quoterightfill

\tl_new:N \tagpos
\tl_new:N \anchorpos

\NewDocumentCommand{\myquote}{m m m}{
    \noindent

    \if_case:w \int_mod:nn {#1} {2}
        \tl_gset:Nn \quoteleftfill \hfill
        \tl_gset:Nn \quoterightfill {}

        \tl_gset:Nn \tagpos {east}
        \tl_gset:Nn \anchorpos {west}

    \else:
        \tl_gset:Nn \quoteleftfill {}
        \tl_gset:Nn \quoterightfill \hfill\null

        \tl_gset:Nn \tagpos {west}
        \tl_gset:Nn \anchorpos {east}
    \fi:

    \quoteleftfill

     \begin{tikzpicture}[outer~sep=4pt]
        %\node [draw] (T) {%<-- uncomment to see the box and comment the next line
        \node  (T) {%
            \begin{minipage}{0.8\linewidth}%
                \textit{#2}
            \end{minipage}};

        \node at (T.south~\tagpos)[red,anchor=south~\anchorpos]
            {\Large\bfseries #1};

        \draw [line~width=2pt](T.north~\tagpos) -- (T.south~\tagpos);

        \node at (T.south)[below]{%
            \begin{minipage}{0.8\linewidth}%
                \begin{center}
                    \textbf{\textit{---~#3}}
                \end{center}
            \end{minipage}};
    \end{tikzpicture}

    \quoterightfill
}
\ExplSyntaxOff

EDIT2、3 和 4:使用 projetmbc 的代码和 OP 的评论

  • 选项中的引号宽度(默认为 0.8\linewidth)

编辑3 引文在节点中起搏。 在此处输入图片描述

\node at (T.south~\tagpos)[red,anchor=south~\anchorpos]
              {\Large\bfseries #2};

其中\tagpos包含西方或东方(偶数或奇数)

我们将包含命令中传递的数字的 #2 放在\myquote左下角或右下角。

为了得到你想要的东西,你必须移除南方。

替换\node at (T.south~\tagpos)[red,anchor=south~\anchorpos]{\Large\bfseries #2};\node at (T.\tagpos)[red,anchor=\anchorpos]{\Large\bfseries #2};

代码编辑4

        \documentclass{article}
%https://tex.stackexchange.com/questions/670763/how-to-add-numbers-next-to-the-frame-line-using-mdframed
\usepackage{showframe}%<--- comment in the final doc
\usepackage{xcolor}
\usepackage{tikz}
\ExplSyntaxOn

\tl_new:N \quoteleftfill
\tl_new:N \quoterightfill

\tl_new:N \tagpos
\tl_new:N \anchorpos

\NewDocumentCommand{\myquote}{O{0.8\linewidth} m m m}{
  % #1 the widtn (option)
  % #2 the number
  % #3 the quote
  % #4 the author
  %%%%%%%%%%%
    \noindent

    \if_case:w \int_mod:nn {#2} {2}
        \tl_gset:Nn \quoteleftfill \hfill
        \tl_gset:Nn \quoterightfill {}

        \tl_gset:Nn \tagpos {east}
        \tl_gset:Nn \anchorpos {west}

    \else:
        \tl_gset:Nn \quoteleftfill {}
        \tl_gset:Nn \quoterightfill \hfill\null

        \tl_gset:Nn \tagpos {west}
        \tl_gset:Nn \anchorpos {east}
    \fi:

    \quoteleftfill

    \begin{tikzpicture}[outer~sep=4pt]
        %\node [draw] (T) {%<-- uncomment to see the box and comment the next line
        \node  (T) {%
            \begin{minipage}{#1}%
                \textit{#3}
            \end{minipage}};

        \node at (T.\tagpos)[red,anchor=\anchorpos]
            {\Large\bfseries #2};

        \draw [line~width=2pt](T.north~\tagpos) -- (T.south~\tagpos);

        \node at (T.south)[below]{\textbf{\textit{---~#4}}};
    \end{tikzpicture}

    \quoterightfill
}
\ExplSyntaxOff

\begin{document}

\myquote{1}{A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.}{Winston Churchill}

\myquote{2}{Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did. So throw off the bowlines, sail away from the safe harbor. Catch the trade winds in your sails.}{Marc Twain}
\end{document}

备注 节点的宽度默认为线宽的 80%,您可以通过将此参数放在方括号(选项)之间来选择其他宽度。例如:

\myquote[5cm]{1}{A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.}`{Winston Churchill}

在此处输入图片描述

答案2

这是另一种基于tcolorbox而不是 的解决方案mdframed。以下代码提出了一个myquote具有与引用作者相对应的强制参数的环境。该框包含一个自动更新的计数器,它也会自动放置在左侧或右侧,感谢muzimuzhi Z的帮助。

\documentclass[11pt,twoside,openright]{book}
\usepackage[most]{tcolorbox}

\makeatletter
\newtcolorbox[auto counter]{myquote}[2][]{
    enhanced,
    sharp corners,
    flip title={sharp corners},
    title={--- #2},
    fonttitle=\bfseries\itshape,
    halign title=center,
    fontupper=\itshape,
    colback=white,
    colbacktitle=white,
    coltitle=black,
    boxrule=0pt,
    boxsep=0pt,
    top=0pt,
    bottom=0pt,
    left=3mm,
    right=3mm,
    toptitle=3mm,
    bottomtitle=3mm,
    /utils/if odd={\value{tcb@cnt@myquote}+1}%
        {overlay={%
            \draw[black, line width=2pt] (interior.north west)--(title.north west) node[left, red, midway]{\thetcbcounter};}}%
        {overlay={%
            \draw[black, line width=2pt] (interior.north east)--(title.north east) node[right, red, midway]{\thetcbcounter};}},
}
\makeatother

\pgfkeys{
  /utils/if odd/.code n args={3}{%
    \ifodd\numexpr#1\relax
      \pgfkeysalso{#2}%
    \else
      \pgfkeysalso{#3}%
    \fi
  }
}


\begin{document}

\begin{myquote}{Winston Churchill}
A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.
\end{myquote}

\begin{myquote}{Winston Churchill}
A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.
\end{myquote}

\begin{myquote}{Winston Churchill}
A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.
\end{myquote}
\end{document}

在此处输入图片描述

答案3

另一种方法是使用表格将数字放在线的中心。

  • 使用计数器mycnta(参见文档tabularray第 47 页)

  • 每句引言各有三行,

    1 行,带编号,引文 2 空行 3 作者

  • 手动管理垂直线vline{3} = {1,7}{1pt}(偶数引号)、vline{2} = {4}{1pt}(奇数引号)

代码

\documentclass{article}
%https://tex.stackexchange.com/questions/670763/how-to-add-numbers-next-to-the-frame-line-using-mdframed
\usepackage{showframe}
\usepackage{tabularray}
%\usepackage{xcolor}
%%%%%%%%%%%%%%%%%%%%%%    in the doc tabularray p.47
\UseTblrLibrary{counter}
\newcounter{mycnta}
\newcommand{\mycnta}{\stepcounter{mycnta}\arabic{mycnta}}
%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\setcounter{mycnta}{0}
\begin{center}
\begin{tblr}{
    colspec={Q[0.42\linewidth,valign=b]Q[1em,c,fg=red,font=\Large\bfseries]Q[0.42\linewidth,valign=b]},
    colsep=4pt,
    vline{3} = {1,7}{1pt},%<---   line 1 and 7 , vertical line colomn 3
    vline{2} = {4}{1pt},
    %hline{1,4,7}%<--- uncomment for the horizontal lines
}
    &\mycnta
    &
    A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool. It is a good thing for an uneducated man to read books of quotations.\\
    \\
    && \centering --- \textbf{\textit{Winston Churchill}}\\

    Twenty years from now you will be more disappointed by the things you didn't do than by the ones you did. So throw off the bowlines, sail away from the safe harbor. Catch the trade winds in your sails.
    &\mycnta\\
    \\
    \centering --- \textbf{\textit{Marc Twain}}\\
    &\mycnta
    &
    A fine quotation is a diamond in the hand of a man of wit and a pebble in the hand of a fool\dots\\
    \\
    && \centering --- \textbf{\textit{Nobody}}\\
\end{tblr} 
\end{center} 
\end{document}

在此处输入图片描述

相关内容