如何将方括号参数解析为几个花括号参数?

如何将方括号参数解析为几个花括号参数?

我有一个我指定的环境......

\begin{code}{TITLE}{LANGUAGE}{CAPTION}
\end{code}

我希望能够指定它,例如......

\begin{code}[title={TITLE},language={LANGUAGE},caption={CAPTION}]
\end{code}

当前环境的一个例子是......

\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings, breakable, skins}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage[ttdefault=true]{AnonymousPro}
\usepackage{fontawesome5}
\renewcommand*\thelstnumber{\makebox[3em][r]{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}}


% line numbers and white space
\usepackage[space=true]{accsupp}

% create non-copy numbers (source - https://tex.stackexchange.com/a/57160)
\newcommand{\copyablespace}{\BeginAccSupp{method=hex,unicode,ActualText=00A0}\hphantom{x}\EndAccSupp{}}

% copy-able whitespace (source - https://tex.stackexchange.com/a/142765)
\newcommand\donotcopy[1]{% line number printing mechanism
  \BeginAccSupp{ActualText={}}#1\EndAccSupp{}%
}



\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}

\newtcblisting[auto counter]{code}[4][]{
    enhanced,
    top=\smallskipamount,
    bottom=\smallskipamount,
    sharp corners=downhill,
    arc=0.5cm,
    colframe=gray!20,
    attach boxed title to top left={
        % yshift=-\tcboxedtitleheight - \smallskipamount,
        xshift=-2mm
    },
    boxed title style={
        size=small,
        colback=white,
        colframe=white,
        boxrule=0pt,
        top=0pt,
        bottom=0pt,
        right=0pt,
        left=0pt
    },
    fonttitle=\color{black},
    overlay={
        \begin{tcbclipinterior}
            \fill[gray!25](frame.south west)
            rectangle ([xshift=5.0mm]frame.north west);
        \end{tcbclipinterior}
        \node[inner sep=0.75mm, rounded corners=0.5mm, anchor=north east, ,fill=gray!50] at ([xshift=-2.5mm, yshift=\baselineskip]frame.south east){\scriptsize\color{violet}\itshape\ttfamily#2};
        },
    listing only,
    listing options={
        numbers=left, 
        numberstyle=\ttfamily\tiny\donotcopy, 
        captionpos=t,
        abovecaptionskip=0pt,
        belowcaptionskip=\medskipamount,
        language=#3,
        xleftmargin=0.5em,
        basicstyle=\fontfamily{AnonymousPro}\selectfont,
        keywordstyle=\bfseries\color{pblue},
        stringstyle=\bfseries\itshape\color{green!40!black},
        commentstyle=\bfseries\itshape\color{black!60},
        showspaces=false,
        showtabs=false,
        breaklines=true,
        showstringspaces=false,
        tabsize=1,
        emphstyle={\bfseries\color{pblue}},
        columns=fullflexible,
        literate={\ }{{\copyablespace}}1,
    },
    title=\ifx #4\empty\empty\else\ \textbf{Code Snippet~\thetcbcounter.} #4\fi,
    #1
}

\setlength\parindent{0pt}

\begin{document}

\begin{code}{\faJava \ \raisebox{0.1\height}{Hello.Java}}{Java}{A Java Example.}
String s = "Hello World";

Animation from_top = AnimationUtils.loadAnimation(this, R.anim.from_top);
imageView.setAnimation(from_top);

// change activity with fade animation

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
        finish();
    }
}, SPLASH_TIMEOUT);
\end{code}

\end{document}

答案1

在底层,选项tcblisting被处理为PGF 键。可选参数#1将其内容传递给code环境,这意味着我们可以传入我们自己的 PGF 键模块,我将其命名为codeFormattingKeys。我将这些可选键定义为titlelanguagecaption。当您使用它们时,它们将调用应用了您的样式的预定义键tcblisting

\documentclass{article}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings, breakable, skins}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage[ttdefault=true]{AnonymousPro}
\usepackage{fontawesome5}
\renewcommand*\thelstnumber{\makebox[3em][r]{\ifnum\value{lstnumber}<10 0\fi\the\value{lstnumber}}}


% line numbers and white space
\usepackage[space=true]{accsupp}

% create non-copy numbers (source - https://tex.stackexchange.com/a/57160)
\newcommand{\copyablespace}{\BeginAccSupp{method=hex,unicode,ActualText=00A0}\hphantom{x}\EndAccSupp{}}

% copy-able whitespace (source - https://tex.stackexchange.com/a/142765)
\newcommand\donotcopy[1]{% line number printing mechanism
  \BeginAccSupp{ActualText={}}#1\EndAccSupp{}%
}

\definecolor{pblue}{rgb}{0.13,0.13,1}
\definecolor{pgreen}{rgb}{0,0.5,0}

\newtcblisting[auto counter]{code}[1][]{
    enhanced,
    top=\smallskipamount,
    bottom=\smallskipamount,
    sharp corners=downhill,
    arc=0.5cm,
    colframe=gray!20,
    attach boxed title to top left={
        % yshift=-\tcboxedtitleheight - \smallskipamount,
        xshift=-2mm
    },
    boxed title style={
        size=small,
        colback=white,
        colframe=white,
        boxrule=0pt,
        top=0pt,
        bottom=0pt,
        right=0pt,
        left=0pt
    },
    fonttitle=\color{black},
    listing only,
    listing options={
        numbers=left, 
        numberstyle=\ttfamily\tiny\donotcopy, 
        captionpos=t,
        abovecaptionskip=0pt,
        belowcaptionskip=\medskipamount,
        xleftmargin=0.5em,
        basicstyle=\fontfamily{AnonymousPro}\selectfont,
        keywordstyle=\bfseries\color{pblue},
        stringstyle=\bfseries\itshape\color{green!40!black},
        commentstyle=\bfseries\itshape\color{black!60},
        showspaces=false,
        showtabs=false,
        breaklines=true,
        showstringspaces=false,
        tabsize=1,
        emphstyle={\bfseries\color{pblue}},
        columns=fullflexible,
        literate={\ }{{\copyablespace}}1,
    },
    /codeFormattingKeys/.cd,#1,/tcb/.cd
}

\pgfkeys{
  /codeFormattingKeys/title/.style    = {/tcb/title={\ifx #1\empty\empty\else\ \textbf{Code Snippet~\thetcbcounter.} #1\fi}},
  /codeFormattingKeys/language/.style = {/tcb/listing options app={language=#1}},
  /codeFormattingKeys/caption/.style  = {
    /tcb/overlay={
      \begin{tcbclipinterior}
        \fill[gray!25](frame.south west)
        rectangle ([xshift=5.0mm]frame.north west);
      \end{tcbclipinterior}
      \node[inner sep=0.75mm,rounded corners=0.5mm,anchor=north east,fill=gray!50] at ([xshift=-2.5mm, yshift=\baselineskip]frame.south east){\scriptsize\color{violet}\itshape\ttfamily#1};
    },
  }
}

\setlength\parindent{0pt}

\begin{document}

\begin{code}[
  title=A Java Example,
  language=Java,
  caption={\faJava \ \raisebox{0.1\height}{Hello.Java}},
]
String s = "Hello World";

Animation from_top = AnimationUtils.loadAnimation(this, R.anim.from_top);
imageView.setAnimation(from_top);

// change activity with fade animation

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        startActivity(intent);
        overridePendingTransition(R.anim.fade_in,R.anim.fade_out);
        finish();
    }
}, SPLASH_TIMEOUT);
\end{code}

\end{document}

相关内容