如何使用 bicaption 以及如何为 lstlisting 定义双语名称?

如何使用 bicaption 以及如何为 lstlisting 定义双语名称?

我需要为论文中的代码写双语标题,但我不知道如何\bicaption使用 lstlisting

另外,我想将双标题中的名称更改lstlisting为代码。

我知道我可以用来\renewcommand{\lstlistingname}{}重新定义的名称lstlisting,但如果我需要用两种语言更改它,我不知道如何重命名它。

% !TeX encoding = UTF-8
\documentclass{article}
\usepackage[UTF8]{ctex}

\RequirePackage{caption}
\RequirePackage[list=off]{bicaption}
\RequirePackage{subcaption}

\usepackage{listings}
\usepackage{bicaption}

\AtBeginDocument{\renewcommand*{\thelstlisting}{\thechapter-\arabic{lstlisting}}}

\begin{document}
    \begin{table}
        \centering
        \bicaption{表格测试}{Table Test}
        \begin{tabular}{|c|c|}
            \hline
            a&b\\
            \hline
            a&b\\
            \hline
            a&b\\
            \hline
        \end{tabular}
    \end{table}
    \lstset {
        language=C
    }
    \begin{figure}
        \centering
        \begin{lstlisting}[caption=Test example,captionpos=b]
            int foo() {
                return 0;
            }
        \end{lstlisting}
    \end{figure}
\end{document}

这是一个例子,我想为列表添加两个标题,就像表格一样。

以下是使用的软件包及其版本的列表。

 article.cls    2020/04/10 v1.4m Standard LaTeX document class
  size10.clo    2020/04/10 v1.4m Standard LaTeX file (size option)
    ctex.sty    2020/10/19 v2.5.5 Chinese adapter in LaTeX (CTEX)
   expl3.sty    2020-10-27 L3 programming layer (loader) 
l3backend-xetex.def    2020-09-24 L3 backend support: XeTeX
  xparse.sty    2020-10-27 L3 Experimental document command parser
l3keys2e.sty    2020-10-27 LaTeX2e option processing using LaTeX3 keys
ctexhook.sty    2020/10/19 v2.5.5 Document and package hooks (CTEX)
ctexpatch.sty    2020/10/19 v2.5.5 Patching commands (CTEX)
  fix-cm.sty    2015/01/14 v1.1t fixes to LaTeX
  ts1enc.def    2001/06/05 v3.0e (jk/car/fm) Standard LaTeX file
everysel.sty    2011/10/28 v1.2 EverySelectfont Package (MS)
ctexopts.cfg    2020/10/19 v2.5.5 Option configuration file (CTEX)
ctex-engine-xetex.def    2020/10/19 v2.5.5 XeLaTeX adapter (CTEX)
   xeCJK.sty    2020/10/19 v3.8.6 Typesetting CJK scripts with XeLaTeX
xtemplate.sty    2020-10-27 L3 Experimental prototype document functions
fontspec.sty    2020/02/21 v2.7i Font selection for XeLaTeX and LuaLaTeX
fontspec-xetex.sty    2020/02/21 v2.7i Font selection for XeLaTeX and LuaLaTeX
 fontenc.sty
fontspec.cfg
   xeCJK.cfg    2020/10/19 v3.8.6 Configuration file for xeCJK package
zhnumber.sty    2020/05/01 v2.8 Typesetting numbers with Chinese glyphs
zhnumber-utf8.cfg    2020/05/01 v2.8 Chinese numerals with UTF8 encoding
ctex-scheme-chinese.def    2020/10/19 v2.5.5 Chinese scheme for generic (CTEX)
ctex-name-utf8.cfg    2020/10/19 v2.5.5 Caption with encoding UTF-8 (CTEX)
indentfirst.sty    1995/11/23 v1.03 Indent first paragraph (DPC)
ctex-c5size.clo    2020/10/19 v2.5.5 c5size option (CTEX)
ctex-fontset-windows.def    2020/10/19 v2.5.5 Windows fonts definition (CTEX)
    ctex.cfg    2020/10/19 v2.5.5 Configuration file (CTEX)
 caption.sty    2020/10/26 v3.5g Customizing captions (AR)
caption3.sty    2020/10/21 v2.2e caption3 kernel (AR)
  keyval.sty    2014/10/28 v1.15 key=value parser (DPC)
bicaption.sty    2020/10/25 v1.3 Bilingual Captions (AR)
subcaption.sty    2020/10/07 v1.3j Sub-captions (AR)
listings.sty    2020/03/24 1.8d (Carsten Heinz)
 lstmisc.sty    2020/03/24 1.8d (Carsten Heinz)
listings.cfg    2020/03/24 1.8d listings configuration
xeCJK-listings.sty    2020/10/19 v3.8.6 xeCJK patch file for listings
  xpatch.sty    2020/03/25 v0.3a Extending etoolbox patching commands
etoolbox.sty    2020/10/05 v2.5k e-TeX tools for LaTeX (JAW)
lstlang1.sty    2020/03/24 1.8d listings language file
 lstmisc.sty    2020/03/24 1.8d (Carsten Heinz)

答案1

用法:

  • listings选择bicaption=<caption text>
  • bicaption 的名称lstlisting存储在\lstlistingnameBi
\documentclass{article}
\usepackage{listings}
\usepackage{bicaption}
\usepackage{xpatch}

\makeatletter

% define new option "bicaption"
\lst@Key{bicaption}\relax{\lstKV@OptArg[{#1}]{#1}%
  {\def\lst@bicaption{##2}\def\lst@@bicaption{##1}}}
% init option
\lst@AddToHookExe{TextStyle}
  {\let\lst@bicaption\@empty \let\lst@@bicaption\@empty}

% new caption name
\def\lstlistingnameBi{Listing} % the name "Listing" in second language
\def\fnum@lstlisting@bi{%
  {\lstlistingnamestyle\lstlistingnameBi
  \ifx\lst@@caption\@empty\else~\thelstlisting\fi}}

% patch (the "caption" patched) \lst@MakeCaption to insert bicaption
\AtBeginDocument{
  \xpatchcmd\caption@ORI@lst@MakeCaption
    {\lst@makecaption\fnum@lstlisting{\ignorespaces \lst@caption}}
    {\lst@makecaption\fnum@lstlisting{\ignorespaces \lst@caption}%
     \ifx\lst@bicaption\@empty
     \else
       \lst@makecaption\fnum@lstlisting@bi{\ignorespaces \lst@bicaption}%
     \fi}
    {}{\fail}
}
\makeatother

\begin{document}

\renewcommand{\lstlistingnameBi}{Listing Bi Cap}
\begin{lstlisting}[
  basicstyle=\ttfamily,captionpos=b,language=C,gobble=2,
  caption=first caption, bicaption=second caption
]
  int foo() {
      return 0;
  }
\end{lstlisting}
\end{document}

在此处输入图片描述

相关内容