TransWikia.com

newtcblisting with minted AND caption below listing

TeX - LaTeX Asked by fullStackChris on August 15, 2021

I have found these nicely formatted tcblistings from this post:

Bash terminal example.

or

Example of code from a React component.

Currently, I’m working with this to generate such boxes:

AtBeginDocument{
newtcblisting[blend into=listings]{codeInput}[3]{
  listing engine=minted,
  minted language=#1,
  minted options={autogobble,breaklines,breakanywhere,bgcolor=codebackground,breaksymbolleft=,breaksymbolright=,fontsize=footnotesize},
  listing only,
  size=title,
  arc=1.5mm,
  breakable,
  enhanced jigsaw,
  colframe=Black,
  coltitle=White,
  boxrule=0.5mm,
  colback=codebackground,
  coltext=Black,
  title=TwoSymbolsAndText{faCode}{%
    footnotesize{texttt{#2}}
  }{faCode},
  list text=#3
}
}

In my case, I am using the ‘title’ as the file name or environment where the code lives. My LaTeX code looks like this:

begin{codeInput}{bash}{terminal}{Installing Gatsby via npm.}
npm install gatsby
end{codeInput}
begin{codeInput}{jsx}{Layout.tsx}{Adding the Nav component to Layout.tsx.}
...
import { Nav } from "./Nav" // new
...
return (
  <>
    <Nav siteTitle={data.site.siteMetadata.title} /> // new
    <main>{children}</main>
  </>
)
...
end{codeInput}

respectfully for each example above.

Now, I’m able to correctly set the actual text of the listing in my listoflistings by doing list text=#3 in the above LaTeX code, but I would like a caption below these code boxes to match that text. Is there any way to do that, without messing with my nice title bars? I also need to get rid of the ‘Listing X.X’ texts in the title bar – I want an unlabeled title that simply reflects the file name or environment of the code. My current workaround is pretty dumb, looking something like this:

begin{codeInput}{bash}{terminal}{Installing Gatsby via npm.}
npm install gatsby
end{codeInput}
begin{center}
footnotesize{Installing Gatsby via npm.}
end{center}

Which renders a small centered text below the box.

I guess really what I am asking is how to add some custom text – since I think the only way to have a proper caption is with that ‘title’ property in newtcblisting, so then I’ll somehow have to pop that out and replace the content of the title bar with my own text – in my case, the ‘filename’ of the code.

EDIT: A full .tex document of where I’m at:

% set to 'oneside' for web style, 'twoside' for book print
documentclass[paper=6in:9in,pagesize=pdftex,headinclude=on,footinclude=on,12pt]{scrbook}
areaset[0.50in]{4.5in}{8in}

usepackage[dvipsnames]{xcolor}

usepackage{minted}

usepackage[most,minted]{tcolorbox}

usepackage{fontawesome}

usepackage{tabularx}
newcolumntype{CeX}{>{centeringletnewlinearraybackslash}X}%
newcommand{TwoSymbolsAndText}[3]{%
  begin{tabularx}{textwidth}{cCeX c}%
    #1 & #2 & #3
  end{tabularx}%
}

definecolor{codebackground}{HTML}{F2F2F2}

AtBeginDocument{
newtcblisting[blend into=listings]{codeInput}[3]{
  listing engine=minted,
  minted language=#1,
  minted options={autogobble,breaklines,breakanywhere,bgcolor=codebackground,breaksymbolleft=,breaksymbolright=,fontsize=footnotesize},
  listing only,
  size=title,
  arc=1.5mm,
  breakable,
  enhanced jigsaw,
  colframe=Black,
  coltitle=White,
  boxrule=0.5mm,
  colback=codebackground,
  coltext=Black,
  title=TwoSymbolsAndText{faCode}{%
    footnotesize{texttt{#2}}
  }{faCode},
  list text=#3
}
}

begin{document}

listoflistings

begin{codeInput}{bash}{terminal}{Installing Gatsby via npm.}
npm install gatsby
end{codeInput}

begin{codeInput}{jsx}{Layout.tsx}{Adding the Nav component to Layout.tsx.}
...
import { Nav } from "./Nav" // new
...
return (
  <>
    <Nav siteTitle={data.site.siteMetadata.title} /> // new
    <main>{children}</main>
  </>
)
...
end{codeInput}

end{document}

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP