3

I need to write bilingual captions for codes in a paper, but I do not know how to use \bicaption for lstlisting.

Also, I want to change the name of lstlisting to Code in the bicaption.

I know I can use \renewcommand{\lstlistingname}{} to redefine the name of lstlisting, but I do not know how to rename it if I need to change it in two languages.

% !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}

Here is an example, I want to have two captions for the listing just like the table.

Below is the list of used packages and their versions.

 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)
  • Welcome to TeX.SE! Could you provide a small example document that can be used as a starting point? Just a document with documentclass article with a listing in it and maybe a small table with a a bicaption to illustrate the idea. The example should be compilable, i.e., not produce errors and showing the table and the listing when it is compiled. This would make it much easier for people that are interested in answering the question to start looking for solutions. – Marijn Nov 9 '20 at 15:16
  • Thanks for your suggestion, I have added an example here. – Lesterth Nov 9 '20 at 15:33
  • The appended version info DO NOT correspond to your MWE. – muzimuzhi Z Nov 11 '20 at 2:28
  • I use a template in my original document and it contains thousands of codes, so I don't know how to show it in a MWE. – Lesterth Nov 11 '20 at 2:58
  • I'm trying to use a binary search to find the root cause in the template file. – Lesterth Nov 11 '20 at 3:16
3

Usage:

  • new listings option bicaption=<caption text>
  • bicaption name of lstlisting is stored in \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}

enter image description here

  • I get an "Undefined control sequence." error at \fail. If I remove the \fail, the option will not take effect. What's the point? – Lesterth Nov 10 '20 at 2:38
  • I pasted your code into a new file, and I got an "Undefined control sequence." error at \end{lstlisting}. Maybe there is something wrong with the command? – Lesterth Nov 10 '20 at 2:52
  • @Lesterth My code compiles with texlive 2019 & 2020 on overleaf.com. What are the versions of your listings and (bi)caption packages? – muzimuzhi Z Nov 10 '20 at 3:02
  • I use texlive 2018 and bicaption version is 2018/08/26 v1.1-159, caption version is 2018/10/06 v3.3-154, listings version is 2018/09/02 1.7. Maybe I should update them and retry. – Lesterth Nov 10 '20 at 4:18
  • The code works after updating texlive, but I still get the "Undefined control sequence." error at \fail in my original document. I use \AtBeginDocument{\renewcommand*{\thelstlisting}{\thechapter-\arabic{lstlisting}}} to change the numbering and it will cause an error if I insert it into the example. However, even if I remove it in my document, the error still occurs at \fail. – Lesterth Nov 10 '20 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.