这个问题如下我在这里问的那个。我想覆盖tcolorbox
包中的以下自定义列表命令,并能够使用以下功能实现xkeyval
:
- 在嵌套列表环境中定义我想要的语言(使用选项键
language=
), - 能够选择是否需要数字(使用选项键
withNumbers
) - 可以选择是否需要标题(通过选项定义
title
) - 指定一个类型,该类型将在标题之前放置翻译(如果适用)。在可用类型中:
codeSnippet
、、、。和具有数字和标题,其他两种类型没有。并且如果我们处于例如中,我不应该能够指定选项(或者至少该选项必须不执行任何操作并且不能覆盖)。code
config
prompt
codeSnippet
config
title
prompt
type
这个想法是尽可能使用更通用的命令,并且最终命令必须具有所有具有默认值的可选参数(特别是对于语言,如果没有指定,我不想要特定的语言格式),例如:
\begin{code}[withNumbers, title=Hello world.cpp, type=codeSnippet, language=C++]
#include <iostream>
int main(int argc, char *argv[]) {
cout << "hello world" << endl;
}
\end{document}
下午我也尝试了一下让xkeyval有boolkey
某种方式来实现我上面例子中想要展示的效果。
如果键存在,则值为 true,如果不存在,则值为 false。在此示例中,如果我指定withNumbers
,则我想要数字。如果没有指定,则数字应该不存在于最终呈现的列表环境中。
left
如果没有数字,则必须删除标题中的声明,newtcblisting
以避免列表环境左侧出现不必要的移动。
默认情况下,没有语言和数字。对以下代码片段所做的修改不必破坏上述问题带来的功能,也不必使用其他包,除非tcolorbox
我box
将来可能会进一步自定义级别。
我花了一整个下午的时间研究这个xkeyval
包,并通过将其封闭在环境中来定制以下命令\renewcommand
,但根本没有成功:-(
\newtcblisting
使用我谈到的命令的最小工作示例:
\documentclass[oneside]{report}
\usepackage{fontspec}
\setmonofont{Bitstream Vera Sans Mono}[Scale=MatchLowercase]
\usepackage[margin=2.5cm]{geometry}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{breakable}
\tcbuselibrary{skins}
\tcbuselibrary{listings}
\usepackage{caption}
\usepackage{translations}
\DeclareTranslationFallback{codeSnippet}
{Code snippet}
\DeclareTranslation{English}{codeSnippet}
{Code snippet}
\DeclareTranslation{Dutch}{codeSnippet}
{Codefragment}
\DeclareTranslation{French}{codeSnippet}
{Extrait de code}
\usepackage[main=french, german]{babel}
% Let's create style to inherit from in other custom environments based on listings. Indeed, we do not want copy paste always the same customization for each environments.
\lstdefinestyle{custom}{
% NoAutoSpacing prevents a space before colons to be added when the french
% language is loaded with babel.
% src.: https://tex.stackexchange.com/a/117767/66964
% https://tex.stackexchange.com/a/200215/66964
basicstyle=\linespread{1}\normalsize\ttfamily\NoAutoSpacing,
% Space separated words are not properly aligned on next lines if `columns` statement is set to
% flexible. Default is 'fixed'.
columns=fixed,
breaklines=true,
breakautoindent=false,
breakindent=0pt,
% Sets default tabsize to 4 spaces
tabsize=4,
% Normally used to define delimiters in code listings like begin and end
% keywords, but it has a side effect allowing linebreaks. We are thus gonna
% use that ugly trick to force lstinline statements to break the line at
% the underscore character if there is not enough room for the string. This
% is an ugly hack because it has a side effect: the _ character becomes
% a suitable breakpoint for both sides. This can create either foo_\n bar
% or foo\n _bar, but this cannot be avoided.
%moredelim=[s]{_}{_},
% Put in bold words surrounded by @@
% [is] specifies the style.
% i: standing for "invisible", ask for the delimiters @@ to invisible in the rendered version
% s: standing for "string", ask for two delimiters, one starting the expression and the other closing the expression.
moredelim={[is][keywordstyle]{@@}{@@}}
}
% Code environment. Does inherit from lstset settings thanks to style.
\newtcblisting[auto counter]{code}[2][]{%
listing only,
breakable,
top=0.5pt,
bottom=0.5pt,
left=6mm,
sharp corners,
boxrule=0pt,
bottomrule=1pt,
toprule=1pt,
enhanced jigsaw,
listing options={
style=custom,
numbers=left,
numberstyle=\tiny,
},
lefttitle=0pt,
coltitle=black,
colbacktitle=white,
% Crashes on long lstlisting lines. \the\baselineskip reports 12.0pt. 1.2 * 12.0 = 14.4
%borderline north={1pt}{1.2\baselineskip}{dashed},
borderline north={1pt}{14.4pt}{dashed},
title={\GetTranslation{codeSnippet} \thetcbcounter: #2},#1
}
\begin{document}
\begin{code}{Some great title}
one
two
three
four
\end{code}
\end{document}
提前感谢您的帮助。
编辑: 我现在正在将我自己的类文件切换到 pgfkeys(参见我的新问题)。因此您的解决方案可以使用 pgfkeys 而不是 xkeyval。