答案1
更新:我已经研究了这个问题几个小时,检查了tcolorbox
和cleveref
文档(我甚至在这里问了一些与此相关的事情,虽然我最初做的 MWE 很乱而且我后来删除了这个问题),但@cfr 给了我线索,最后我找到了解决你的问题的方法。
您可以创建和使用自己的计数器,并且tcolorbox
包可以在创建框结构时通过给出它们的名称来使用它们(请参阅命令\newtcolorbox
,\newtcbinputlisting
以及\newtcblisting
粘贴的文件中mwe.tex
)。里面title
您必须使用该框(例如属性)\thetcbcounter
来检索刚刚链接的计数器的值。至于交叉引用,\crefname
它是您最好的朋友(请参阅给定mwe.tex
文件中的那些命令)。
欲了解更多信息,请查看tcolorbox
和cleveref
文档。
以下是工作解决方案(文件mwe.tex
):
\documentclass{article}
\newcounter{commentCount}
\newcounter{filePrg}
\newcounter{inputPrg}
\usepackage{geometry}
\geometry{paper=letterpaper,margin=2cm}
\usepackage{ifthen}
\usepackage{fontawesome}
\usepackage[dvipsnames]{xcolor}
\usepackage{tabularx}
\newcolumntype{\CeX}{>{\centering\let\newline\\\arraybackslash}X}%
\newcommand{\TwoSymbolsAndText}[3]{%
\begin{tabularx}{\textwidth}{c\CeX c}%
#1 & #2 & #3
\end{tabularx}%
}
\usepackage{minted}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}
\newtcolorbox[use counter=commentCount, number format=\arabic]{commentBox}[2]{
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=Black,
coltitle=White,
boxrule=0.5mm,
colback=White,
coltext=Black,
title=\TwoSymbolsAndText{\faCheck}{%
\textbf{Comment \thetcbcounter}\ifthenelse{\equal{#1}{}}{}{\textbf{:} \textit{#1}}%
}{\faCheck},
label=comment:#2,
}
\newtcbinputlisting[use counter=filePrg, number format=\arabic]{\codeFromFile}[4]{%
listing engine=minted,
minted language=#1,
listing file={#2},
minted options={autogobble,linenos,breaklines},
listing only,
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=brown,
coltitle=White,
boxrule=0.5mm,
colback=white,
coltext=Black,
title=\TwoSymbolsAndText{\faCode}{%
\textbf{File program \thetcbcounter}\ifthenelse{\equal{#3}{}}{}{\textbf{:} \textit{#3}}%
}{\faCode},
label=filePrg:#4
}
\newtcblisting[use counter=inputPrg, number format=\arabic]{codeInput}[3]{
listing engine=minted,
minted language=#1,
minted options={autogobble,linenos,breaklines},
listing only,
size=title,
arc=1.5mm,
breakable,
enhanced jigsaw,
colframe=brown,
coltitle=White,
boxrule=0.5mm,
colback=white,
coltext=Black,
title=\TwoSymbolsAndText{\faCode}{%
\textbf{Input program \thetcbcounter}\ifthenelse{\equal{#2}{}}{}{\textbf{:} \textit{#2}}%
}{\faCode},
label=inputPrg:#3
}
\usepackage{cleveref}
\crefname{commentCount}{comment}{comments}
\crefname{filePrg}{file program}{file programs}
\crefname{inputPrg}{input program}{input programs}
\begin{document}
\begin{commentBox}{My first comment}{first}
My first comment here! \faTree
\end{commentBox}
\begin{codeInput}{c}{A welcome program.}{code01}
#include <stdio.h>
int main() {
printf("Hello world!\n");
return 0;
}
\end{codeInput}
\codeFromFile{java}{HelloWorld.java}{Another welcome program.}{code01}
See \cref{comment:first} for details. Also, look for \cref{inputPrg:code01} and \cref{filePrg:code01} for further assistance.
\end{document}
这是所HelloWorld.java
使用的文件mwe.tex
(如果您正在运行此示例,请确保此文件位于同一文件夹中mwe.tex
):
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello world!");
}
}
我使用 编译了它latexmk -xelatex -shell-escape mwe.tex
。
以下是编译过程的输出:
希望这可以帮助!
答案2
所以,我搞清楚\tcblistof
了。是标题带有图片导致了问题。有两种方法可以解决此问题:
- 不要在标题定义中使用此图像,而是在覆盖层中使用它。这样可以使标题保持简单,并适合列表。
- 使用 来
list entry
定义列表的标题。
这是适合我需求的解决方案(而且很简洁)。再次感谢 JBFWP286!:-)
\documentclass{article}
\newcounter{filePrg}
\usepackage{geometry}
\geometry{paper=letterpaper,margin=2cm}
\usepackage[dvipsnames]{xcolor}
\usepackage{minted}
\usepackage[most]{tcolorbox}
\tcbuselibrary{listings}
\tcbuselibrary{minted}
\newtcbinputlisting[use counter=filePrg, number within=section, list inside=mypyg]{\codeFromFile}[4]{%
listing engine=minted,
minted language=#1,
listing file={#2},
minted options={autogobble,linenos,breaklines},
listing only,
size=title,
drop fuzzy shadow,
breakable,
enhanced jigsaw,
%arc=1.5mm,
%colframe=brown,
%coltitle=White,
%boxrule=0.5mm,
%colback=white,
%coltext=Black,
title={Listing \thetcbcounter : #3 \hfill%
\smash{\raisebox{-11pt}{\includegraphics[width=1cm,height=1cm]{images/code2.png}}}},
list entry=Listing~\thetcbcounter : #3,
%OR next two lines
%title={Listing \thetcbcounter : #3},
%overlay={\node[anchor=north east,outer sep=-9pt] at ([xshift=-25pt]frame.north east) {\includegraphics[width=1cm,height=1cm]{images/code2.png}}; },
label=lst:#4
}
%http://tex.stackexchange.com/questions/217489/numbering-tcolorbox-toc
\makeatletter % no indent for entries
\renewcommand{\l@tcolorbox}{\@dottedtocline{1}{0pt}{2.3em}}
\makeatother
\usepackage{hyperref}
\usepackage[ngerman,nameinlink]{cleveref}
\crefname{filePrg}{Listing}{Listings}
\begin{document}
\section{Section 1}
\codeFromFile{java}{listings/HelloWorld.java}{Another welcome program.}{code01}
See \cref{lst:code01} for details.
\tcblistof{mypyg}{My Listings}
\end{document}
答案3
我刚刚遇到了标题太长的问题,我的解决方案就不适用了。所以我重新考虑了 JBFWP286 的答案,并对其进行了一些调整(我显然也改变了方框的样式)。以下是它的 MWE:
\documentclass{book}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\newcolumntype{\LeX}{>{\let\newline\\\arraybackslash}X}%
\newcommand{\OneSymbolAndText}[2]{%
\begin{tabularx}{340pt}{l\LeX}%should normally be \textwidth instead of 340pt but fills out the box nicer this way
#1 & #2
\end{tabularx}%
}
\usepackage[most]{tcolorbox}
\newtcolorbox[auto counter,number within=chapter]{Information}[4][]{%
breakable,
enhanced,
arc=0mm,
colback=gray,
colframe=gray,
borderline west={3pt}{0pt}{blue}, % straigh vertical line at the left edge
sharp corners, % No rounded corners
boxrule=0pt, % no real frame,
size=title,
bottom=6pt,
leftupper=12pt,
lefttitle=35pt,
toptitle=6pt,
top=4pt,
list entry=Information~\thetcbcounter : #1,
%fonttitle=\scshape,
fonttitle={\large\bfseries},
coltitle={black}, % Black colour for title
title=\hspace{-36pt}\OneSymbolAndText{\includegraphics[width=1cm,height=1cm]{boximages/infoBulle/info}}{%
\vspace{#4}Information \thetcbcounter : #2},
#1,
label=info:#3
}
\usepackage[ngerman,nameinlink]{cleveref}
\crefname{tcb@cnt@Information}{Information}{Informationen}
\Crefname{tcb@cnt@Information}{Information}{Informationen}
\begin{document}
\begin{Information}{Really really really I mean really long loooooong Title}{labelInf}{-27.5pt}
Test123
\end{Information}
siehe \cref{info:labelInf}
\end{document}
答案4
基于此处先前的答案,我想添加更通用的 MWE。利用该all
选项,tcolorbox
自动加载minted
。
此外,您可以minted
照常使用来设置您的全局设置\setminted
。
使用以下\codeFromFile
命令,您还可以通过以下方式通过单独设置覆盖minted
和设置tcolorbox
\codeFromFile([minted settings]){language}{file}{Title}{label}{additional tcolorbox settings}
\documentclass[paper=letter]{scrartcl}
\usepackage[all]{tcolorbox}
% Arbitrary minted settings
\setminted[latex]{
autogobble,
fontsize=\scriptsize,
linenos=true,
breaklines}
% Arbitrary tcolorbox settings
\tcbset{
list entry=Listing : #4,
fonttitle=\bfseries,
size=fbox,
arc=1mm,
colback=white}
\newtcbinputlisting[list inside=mypyg]{\codeFromFile}[6][]{%
listing engine=minted,
minted options={#1},
minted language=#2,
listing file={#3},
title={\centering #4},
label=#5,
listing only,
breakable,
#6
}{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%
\codeFromFile[fontsize=\large]{latex}{minimal.tex}{Program}{code01}{width=10cm,colframe=red!75!black}
\codeFromFile{latex}{minimal.tex}{Another program}{code02}{}
%
\tcblistof{mypyg}{My Listings}
%
\end{document}
附言:我找不到(可行的)方法来为 tcb 设置添加第二个可选参数。据我所知,您需要\newtcbinputlisting
使用例如重新定义xparse
's ...因此,如果您不交出其他设置\DeclareDocumentCommand
,只需附加一个(请参阅“另一个 brogram”框)。{}
tcolorbox