2

I have an environment which I specify w/...

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

I'd like be able to specify it instead like...

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

A current example for the environment is...

\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}

user41177
  • 375
  • Does this perhaps answer your question? Optional arguments for newtcblisting – gz839918 Sep 04 '23 at 02:34
  • I don't think so. The main issue is that I'd like to be able to change aspects about the listings options from that square bracket arguments list (in particular the caption, and the line number position, line number ranges). – user41177 Sep 04 '23 at 02:58
  • So you want a single optional argument taking several key-value pairs, which are then distributed to two different key-value lists (that of newtcblisting and the nested listing options)? I think its easier to define two optional arguments of which one gets append to newtcblisting the other to listing options. – lukeflo Sep 04 '23 at 05:55

1 Answers1

3

Under the hood, options to a tcblisting are processed as PGF Keys. The optional argument #1 passes its contents to the code environment, which means that we can pass in our own module of PGF Keys, which I've named codeFormattingKeys. I've defined these optional keys as title, language, and caption. When you use them, they'll call on the predefined keys of tcblisting with your styles applied.

\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}

gz839918
  • 1,938