使用 newcommand 自定义 mdfdefinedstyle

使用 newcommand 自定义 mdfdefinedstyle

我目前正在尝试mdfdefinestyle实现不同的颜色,而不必费力调整其样式或逐一重命名它。

这是mdfdefinestyle我使用的

\mdfdefinestyle{mdblackbox}{
    skipabove = 8pt,
    linewidth = 5pt,
    rightline = false,
    leftline = true,
    topline = false,
    bottomline = false,
    linecolor = black,
    backgroundcolor = black!5,
}

我想要实现的是这样的,

\newcommand{\boxcolor}[1]{
    \mdfdefinestyle{mdcustombox}{
        skipabove = 8pt,
        linewidth = 2pt,
        rightline = false,
        leftline = true,
        topline = false,
        bottomline = false,
        linecolor = {#1}, % Retain all adjustment, except color
        backgroundcolor = {#1}!5,
    }
}

这样我就可以通过环境回忆起风格mdframed

\begin{mdframed}[style=mdcustombox, <THE COLOR>]
    Lorem ipsum...
\end{mdframed}

或者类似的东西。我该如何实现呢?

(编辑)希望这是评论中所建议的所需 MWE,

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}
\newcommand{\boxcolor}[1]{
    \mdfdefinestyle{mdcustombox}{
        skipabove = 8pt,
        skipbelow = 8pt,
        linewidth = 2pt,
        rightline = false,
        leftline = true,
        topline = false,
        bottomline = false,
        linecolor = {#1}, % Retain all adjustment, except color
        backgroundcolor = {#1}!5,
    }
}

\begin{document}
    % I wish to have something like this, although the code is currently NOT WORKING

    \begin{mdframed}[style=mdcustombox, <THE COLOR>]
        Lorem ipsum...
    \end{mdframed}
\end{document}

答案1

似乎 mdframed 的 style-key 只是调用\mdfsetup

因此,您可以定义一个具有相同功能的颜色键:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\makeatletter
% In case the key "color" is provided without value, the value "red" is
% assumed.
% In case the key "color" is not specified at all, you get black and white
% unless values for the keys lincolor and backgroundcolor are already
% provided in a superordinate scope and thus are still valid.
%
\@ifdefinable\KV@mdf@color{%
  \define@key{mdf}{color}[red]{%
    \mdfsetup{linecolor = {#1}, backgroundcolor = {#1!5},}%
  }%
}%
\makeatother

\mdfdefinestyle{mdcustombox}{
    skipabove = 8pt,
    skipbelow = 8pt,
    linewidth = 2pt,
    rightline = false,
    leftline = true,
    topline = false,
    bottomline = false,
    % linecolor = {#1}, % Retain all adjustment, except color
    % backgroundcolor = {#1!5},
}


\begin{document}
    \begin{mdframed}[style=mdcustombox, color = blue]
        Lorem ipsum...
    \end{mdframed}

    \begin{mdframed}[color = green, style=mdcustombox]
        Lorem ipsum...
    \end{mdframed}

    \begin{mdframed}[color, style=mdcustombox]
        Lorem ipsum...
    \end{mdframed}

    \begin{mdframed}[style=mdcustombox]
        Lorem ipsum...
    \end{mdframed}
\end{document}

在此处输入图片描述


如果style=mdcustombox一直调用很麻烦,您还可以在键“color”(可以被认为是一种接受值的样式)上定义一个环境来代替样式mdcustombox

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\makeatletter
% In case the key "color" is provided without value, the value "red" is
% assumed.
% In case the key "color" is not specified at all, you get black and white
% unless values for the keys lincolor and backgroundcolor are already
% provided in a superordinate scope and thus are still valid.
%
\@ifdefinable\KV@mdf@color{%
  \define@key{mdf}{color}[red]{%
    \mdfsetup{linecolor = {#1}, backgroundcolor = {#1!5},}%
  }%
}%
\makeatother

\newmdenv[%
    skipabove = 8pt,
    skipbelow = 8pt,
    linewidth = 2pt,
    rightline = false,
    leftline = true,
    topline = false,
    bottomline = false,
]{mdframedcustom}

\begin{document}
    \begin{mdframedcustom}[color = blue]
    % Alternatively you can do:
    %\begin{mdframedcustom}[linecolor = {blue}, backgroundcolor = {blue!5}]
        Lorem ipsum...
    \end{mdframedcustom}

    \begin{mdframedcustom}[color = green]
        Lorem ipsum...
    \end{mdframedcustom}

    \begin{mdframedcustom}[color]
        Lorem ipsum...
    \end{mdframedcustom}

    \begin{mdframedcustom}
        Lorem ipsum...
    \end{mdframedcustom}
\end{document}

在此处输入图片描述


如果仅指定颜色,而无法指定/覆盖其他键的值,则足够了,因此您只需在可选参数中给出颜色规范,而无需color=,您可以执行以下操作:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

% Depending on the age of your TeX-distribution you may need to do:
% \usepackage{expl3}
% \usepackage{xparse}
% \ExplSyntaxOn
% \cs_new_eq:NN \IfBlankF \tl_if_blank:nF
% \cs_new_eq:NN \IfBlankT \tl_if_blank:nT
% \cs_new_eq:NN \IfBlankTF \tl_if_blank:nTF
% \ExplSyntaxOff

\NewDocumentEnvironment{mdframedcustom}{o}{%
  \IfNoValueTF{#1}{%
     % If the optional argument is not provided at all, use defaults
     % of the scope wherein the environment occurs:
     \begin{mdframed}[%
  }{%
     \IfBlankTF{#1}{%
       % If the optional argument is provided blank, as the
       % environment's dedault value use "red":
       \begin{mdframed}[linecolor = {red}, backgroundcolor = {red!5},
     }{%
       % If the optional argument is provided otherwise, (try to)
       % use that:
       \begin{mdframed}[linecolor = {#1}, backgroundcolor = {#1!5},
     }%
  }%
  skipabove = 8pt,
  skipbelow = 8pt,
  linewidth = 2pt,
  rightline = false,
  leftline = true,
  topline = false,
  bottomline = false,
]}
{\end{mdframed}}

\begin{document}
    \begin{mdframedcustom}[blue]
        Lorem ipsum...
    \end{mdframedcustom}

    \begin{mdframedcustom}[green]
        Lorem ipsum...
    \end{mdframedcustom}

    \begin{mdframedcustom}[]
        Lorem ipsum...
    \end{mdframedcustom}

    \begin{mdframedcustom}
        Lorem ipsum...
    \end{mdframedcustom}
\end{document}

在此处输入图片描述

答案2

我会选择一个环境:

\documentclass{article}
\usepackage[framemethod=tikz]{mdframed}
\usepackage{lipsum}

\mdfdefinestyle{mdcustombox}{
  skipabove = 8pt,
  skipbelow = 8pt,
  linewidth = 2pt,
  rightline = false,
  leftline = true,
  topline = false,
  bottomline = false,
}

\newenvironment{custombox}[1]
 {\begin{mdframed}[style=mdcustombox,#1]}
 {\end{mdframed}}

\begin{document}

\begin{mdframed}[style=mdcustombox, linecolor=red!70, backgroundcolor=blue!20]
  Lorem ipsum...
\end{mdframed}

\begin{custombox}{linecolor=red!70, backgroundcolor=blue!20}
  Lorem ipsum...
\end{custombox}

\end{document}

您可以将任何选项传递给mdframed参数custombox

在此处输入图片描述

相关内容