60 请问如何对我的定理环境用latex3进行同类合并?

发布于 2023-04-07 18:59:30

请问如何对我的定理环境中同样的代码合并,意思就是,把需要改动的部分定义为一个token进行封装,开放为类似ctexset那样的latex3键值列表,从而实现增删定理环境时都只需要增删一些键值就可以了,这是我的初步构想,有描述不清的地方还请见谅,但是大抵意思是这样的,因为我不会latex3也还不想学,太难了,所以希望大佬帮帮忙,我认为将这些定理环境封装为一个用户友好型的命令后是非常有用的.
提取码:1234
代码见cls
! 请原谅我总是有些奇怪的构想。

主要是这些需要改动:
1.定理名称与编号
2.定理label
3.定理环境名
4.颜色

还有一个问题,请问image.png
这个盒子的跨页横线如何去掉,就是没有中间两条线段?

查看更多

关注者
1
被浏览
895
雾月
雾月 2023-04-08
这家伙很懒,什么也没写!
\documentclass{ctexbook}
\usepackage[left=2cm,right=2cm,top=.6cm,bottom=2.2cm]{geometry}
\usepackage{amsmath,amsthm}
\usepackage{xcolor,ninecolors}
\usepackage{tikz}
\usetikzlibrary{shadows}
\usepackage[most]{tcolorbox}

\usepackage{zhlipsum}

\makeatletter

\definecolor{CyaN}{HTML}{dbe1e5}
\colorlet{OrangE}{yellow!20!orange}
\colorlet{BluE}{cyan!70!blue}
\colorlet{ReD}{red!20!orange}
\colorlet{GreeN}{yellow!40!green}
\definecolor{nuanbai}{HTML}{f5f5f5}

\tcbset{my@basic@thm/.style={enhanced jigsaw, breakable, 
  enlarge left by=-3.5mm, width=\textwidth+3.5mm, boxrule=0pt, 
  top=2pt, bottom=2pt, left=2.5mm, frame hidden}}
\newcommand{\@my@newtheorem@n}[1]{\ERROR}
\newcommand{\@my@newtheorem@nn}[2]{%
  \@ifundefined{#1}{\@my@newtheorem{#1}#2}{\@my@renewtheorem{#1}#2}}
\NewDocumentCommand{\@my@newtheorem}{ m m o +m }
  {\IfNoValueTF{#3}{\newtheorem{#1}{#2}}{\newtheorem{#1}{#2}[#3]}%
    \tcbset{my@#1@thmstyle/.style={#4}}%
    \tcolorboxenvironment{#1}{my@basic@thm, my@#1@thmstyle}}
\NewDocumentCommand{\@my@renewtheorem}{ m m o +m }
  {\IfNoValueTF{#3}{\renewtheorem{#1}{#2}}{\renewtheorem{#1}{#2}[#3]}%
    \tcbset{my@#1@thmstyle/.style={#4}}}

\tcbset{my@basic@tcbthm/.style={detach title,}}
\newcommand{\@my@newtcbtheorem@n}[1]{\@my@newtcbtheorem@nn{#1}{}}
\newcommand{\@my@newtcbtheorem@nn}[2]{%
  \@ifundefined{#1}{\@my@newtcbtheorem{#1}{#2}}{\@my@renewtcbtheorem{#1}{#2}}}
\newcommand{\@my@newtcbtheorem}[2]{%
  \tcbset{my@#1@tcbthmstyle/.style={#2}}%
  % 这个地方把标题保存到 \tcbtitletext 里,在后面的 overlay 需要对应修改,
  % 其它不变的内容放在单独的样式里就好
  \NewTColorBox{#1}{ O{} O{} o }
    {my@basic@tcbthm, my@#1@tcbthmstyle, title={##2}, IfNoValueF={##3}{label={##3}}, ##1}}
\newcommand{\@my@renewtcbtheorem}[2]{\tcbset{my@#1@tcbthmstyle/.style={#2}}}

\pgfkeys{/mytcbtheorem/.is family}
\newcommand{\mytcbtheoremset}{\pgfqkeys{/mytcbtheorem}}

\ExplSyntaxOn
\cs_new_protected:Npn \mynewtheorem 
  { \keyval_parse:NNn \@my@newtheorem@n \@my@newtheorem@nn }
\cs_new_protected:Npn \mynewtcbtheorem
  { \keyval_parse:NNn \@my@newtcbtheorem@n \@my@newtcbtheorem@nn }
\ExplSyntaxOff
\makeatother

%% 定义定理
\mynewtheorem{
  defi={\textbf{定义}}[section]{colback=CyaN!80, borderline west={1.5mm}{0mm}{ReD}},
  thm={\textbf{定理}}[section]{colback=CyaN, borderline west={1.5mm}{0mm}{CyaN!80!black}},
  lem={\textbf{引理}}[section]{colback=CyaN!80, borderline west={1.5mm}{0mm}{BluE}},
  prop={\textbf{命题}}[section]{colback=CyaN!80, borderline west={1.5mm}{0mm}{OrangE}},
  exam={\textbf{题}}[chapter]{colback=CyaN!80, borderline west={1.5mm}{0mm}{DarkGreen}},
  cor={\textbf{推论}}[chapter]{colback=CyaN!80, borderline west={1.5mm}{0mm}{violet}},
}
\renewcommand{\proofname}{\textbf{证明.}}
\newtheorem*{remark}{\textbf{注}}

\makeatletter
\newcommand{\my@theorem@overlay@unbroken}{
      %%============== First ==============%% 
  \fill[black!60] ([xshift=1cm,yshift=5pt]frame.north west)-- ([xshift=0.8cm,yshift=0cm]frame.north west)--([xshift=1.2cm,yshift=0cm]frame.north west)--cycle;
  \node[rectangle, text=white, drop shadow={opacity=.3, shadow xshift=0.1cm}, inner sep=1.5mm,fill=purple5,
anchor=west,rounded corners=3pt,font=\normalsize] at ([xshift=0cm,yshift=-3.mm]frame.north west) {\phantom{\rule{1.2em}{0pt}}\bf   \Theorem\ \thetcbthm.~\tcbtitletext};
  \fill[color=purple5,drop shadow={opacity=0.3,shadow xshift=.3pt}] 
      ([xshift=1cm,yshift=5pt]frame.north west) {[rounded corners=3pt]--++(-1cm-5pt,0)}--++(0,-1cm-5pt)--cycle;
      \fill[color=black!60] ([xshift=-5pt,yshift=-1cm]frame.north west)--([xshift=0cm,yshift=-0.8cm]frame.north west)-- ([xshift=0cm,yshift=-1.2cm]frame.north west)--cycle;
      %%============== End ==============%% 
  \fill[purple5,drop shadow={opacity=0.3,shadow xshift=-0.008\linewidth,shadow yshift=0.0016\linewidth}] 
  ([xshift=-0.05\linewidth,yshift=-5pt]frame.south east) {[rounded corners=3pt]--++(0.05\linewidth+5pt,0)}--++(0,0.05\linewidth+5pt)--cycle;
  \fill[black!60] ([xshift=-0.05\linewidth,yshift=-5pt]frame.south east) -- ([xshift=-0.04\linewidth,yshift=0pt]frame.south east)-- ([xshift=-0.06\linewidth,yshift=0pt]frame.south east)--cycle;
  \fill[black!60] ([xshift=5pt,yshift=0.05\linewidth]frame.south east)-- ([yshift=0.04\linewidth]frame.south east) -- ([yshift=0.06\linewidth]frame.south east)--cycle;
}
\newcommand{\my@theorem@overlay@first}{
  \fill[black!60] ([xshift=1cm,yshift=5pt]frame.north west)-- ([xshift=0.8cm,yshift=0cm]frame.north west)--([xshift=1.2cm,yshift=0cm]frame.north west)--cycle;
  \node[rectangle, text=white, drop shadow={opacity=.3, shadow xshift=0.1cm}, inner sep=1.5mm,fill=purple5, anchor=west,rounded corners=3pt,font=\normalsize] at ([xshift=0cm,yshift=-3.mm]frame.north west) {\phantom{\rule{1.2em}{0pt}}\bf  \Theorem\ \thetcbthm.~\tcbtitletext};
  \fill[color=purple5,drop shadow={opacity=0.3,shadow xshift=.3pt}] 
        ([xshift=1cm,yshift=5pt]frame.north west) {[rounded corners=3pt]--++(-1cm-5pt,0)}--++(0,-1cm-5pt)--cycle;
  \fill[color=black!60] ([xshift=-5pt,yshift=-1cm]frame.north west)--([xshift=0cm,yshift=-0.8cm]frame.north west)-- ([xshift=0cm,yshift=-1.2cm]frame.north west)--cycle;
}
\newcommand{\my@theorem@overlay@last}{
  \fill[purple5,drop shadow={opacity=0.3,shadow xshift=-0.008\linewidth,shadow yshift=0.0016\linewidth}] 
  ([xshift=-0.05\linewidth,yshift=-5pt]frame.south east) {[rounded corners=3pt]--++(0.05\linewidth+5pt,0)}--++(0,0.05\linewidth+5pt)--cycle;
  \fill[black!60] ([xshift=-0.05\linewidth,yshift=-5pt]frame.south east) -- ([xshift=-0.04\linewidth,yshift=0pt]frame.south east)-- ([xshift=-0.06\linewidth,yshift=0pt]frame.south east)--cycle;
  \fill[black!60] ([xshift=5pt,yshift=0.05\linewidth]frame.south east)-- ([yshift=0.04\linewidth]frame.south east) -- ([yshift=0.06\linewidth]frame.south east)--cycle;
}
\newcounter{tcbthm}
\mytcbtheoremset{
  theorem name/.store in=\Theorem,
  theorem autoref name/.store in=\tcbthmautorefname,
  theorem the counter/.store in=\thetcbthm,
  theorem style/.code=\tcbset{my@theorem@tcbthmstyle/.style={#1}},
}
% 定义和设置tcb定理
\mytcbtheoremset{
  theorem name=定理,
  theorem autoref name=\bfseries 定理,
  theorem the counter=\thesection.\arabic{tcbthm},
  % theorem style={}, % 改变样式
}
\mynewtcbtheorem{
  theorem={
    arc=3pt,breakable,enhanced,colback=nuanbai,boxrule=0pt,top=8mm,
    fuzzy shadow={-0.6mm}{0.6mm}{0mm}{0.3mm}{white!50!gray},% 上
    fuzzy shadow={0.6mm}{-0.6mm}{0mm}{0.3mm}{fill=white!40!gray},%下
    opacityframe=0, opacityback=0.98,
    fontupper=\itshape, step={tcbthm},
    before pre=\smallskip, after app=\smallskip,
    overlay unbroken=\my@theorem@overlay@unbroken,
    overlay first=\my@theorem@overlay@first,
    overlay last=\my@theorem@overlay@last,
  },
}
\makeatother

\begin{document}

\chapter{章}
\section{结}

\zhlipsum[1][name=zhufu]

\begin{thm}
\zhlipsum[1][name=zhufu]
\end{thm}

\begin{theorem}
\zhlipsum[2][name=zhufu]
\end{theorem}

\end{document}

这样定义的和你的效果一样:
image.png

把长的代码保存到一个命令里,把重复使用的保存到一个命令(或 style)里。

用到 LaTeX3 的只有一个地方。键值设置可以用 pgfkeys 实现,不需要用 LaTeX3。

下一个问题和这个问题无关,应该分别提问。
我修改了代码,可以使用 ys/frame/not at break 这个键来设置不显示断开位置的线,还增加了设置透明度的选项,用法如下:

\begin{tcolorbox}[enhanced,ys style,breakable,pad at break=2mm,
  ys/frame/not at break,ys/frame/fill opacity=0.2]
\zhlipsum[2-20][name=zhufu]
\end{tcolorbox}

代码见 https://gitee.com/Sophanatprime/SomeTeXMacro/blob/main/style/tcbstyle1.tex
https://github.com/Sophanatprime/SomeTeXMacro/blob/main/style/tcbstyle1.tex

2 个回答
烟云幻梦
烟云幻梦 2023-04-08
数学系咸鱼一枚!

老师, 还有个小问题,那个您的新代码盒子里面我加的一些小装饰给去掉了,您看下能否加回来啊?就是这样的
image.png

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览