[Tex/LaTex] lstlisting caption problem with xepersian

captionslistingsxepersian

I am using the xepersian and lstlisting packages and I am having some trouble with the captions.

Here is the code:

\documentclass[a4paper,10pt]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{color}

\usepackage{xepersian}
\settextfont{Nazli}
%\setlatintextfont{Courier}
\DefaultMathsDigits

\definecolor{customblue}{RGB}{235,241,245}
\definecolor{light-gray}{gray}{0.95}
\lstdefinestyle{C++Style}{%
  backgroundcolor=\color{customblue},
  breaklines=true,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\color{blue},
  commentstyle=\color{OliveGreen}\textit,
  stringstyle=\color{red},
  numbers=left,
  numberstyle={\tiny\lr},
  showspaces = false,
  showstringspaces = false,
  tabsize = 2,
  frame=single,
  xleftmargin=5pt,
  xrightmargin=3pt,
  language =  C++,
  aboveskip = 20pt,
  rulecolor=\color{black},
}

\lstnewenvironment{C++Code}
{
   \lstset{style=C++Style}
}{}

\def\lstlistingname{برنامه}

\begin{document}

%case 1
\begin{LTR}
\begin{C++Code}
int main()
{
  return 0;
}
\end{C++Code}
\end{LTR}

%case 2
\begin{LTR}
\begin{lstlisting}[style=C++Style,caption=\rl{تابع فلان}]
int main()
{
  return 0;
}
\end{lstlisting}
\end{LTR}

\end{document}

In case 1, the caption does not apear at all and there is a compile-time warning "text dropped after begin of listing …"

In case 2, The caption does appear but the orientaion is wrong. The whole caption including the \lstlistingname #: bit must be right-to-left.

I tried to use something like:

\lstnewenvironment{C++CodeLTR}
{
  \LTR
 {
%    \lstset{style=C++Style}
 }
}{}
% \lstset{style=C++Style}

but it doesn't work.

P.S. By the way, in case 1, the style is not applied either.

P.P.S. Would these lines from listings-xepersian.def help anyone figure out the problem?

\ProvidesFile{listings-xepersian.def}[2014/07/17 v0.3 bilingual captions for listings package]
\def\lstlistingname{\if@RTL برنامهٔ\else Listing\fi}
\def\lstlistlistingname{\if@RTL فهرست برنامه‌ها\else Listings\fi}
\endinput

I thought this might be relevant since if I don't explicitly define lstlistingname, its value would be equal to Listing. Hence the conditions are not met.

Best Answer

You need to set the environment up to accept an optional argument if you wish to use one. (This problem has nothing to do with the direction of typesetting.) There is an example on page 42 of the manual for listings.

However, to get the caption typeset correctly, you need to look at page 18 of the manual for bidi which explains the problem and its solution specifically for the case you are interested in i.e. RTL caption (such that generic name -> number -> colon -> specific name) with LTR code box. For this, bidi provides an additional key for listings, captiondirection which you may set to one of three values: RTL, LTR or textdirection. Since you need to switch to LTR to typeset the code, but want to retain RTL for the captions, you want to use RTL to override the current direction of typesetting.

\documentclass[a4paper,10pt]{article}
\usepackage{listings}
\usepackage[usenames,dvipsnames]{xcolor}

\usepackage{xepersian}
\settextfont{Noto Kufi Arabic}%{PakType Naqsh} http://code.google.com/p/noto/
\DefaultMathsDigits

\definecolor{customblue}{RGB}{235,241,245}
\definecolor{light-gray}{gray}{0.95}
\lstdefinestyle{C++Style}{%
  backgroundcolor=\color{customblue},
  breaklines=true,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\color{blue},
  commentstyle=\color{OliveGreen}\textit,
  stringstyle=\color{red},
  numbers=left,
  numberstyle={\tiny\lr},
  showspaces = false,
  showstringspaces = false,
  tabsize = 2,
  frame=single,
  xleftmargin=5pt,
  xrightmargin=3pt,
  language =  C++,
  aboveskip = 20pt,
  rulecolor=\color{black},
  captiondirection=RTL,
}

\lstnewenvironment{C++Code}[1][]
{%
  \lstset{style=C++Style, #1}%
}{%
}

\def\lstlistingname{برنامه}


\begin{document}

  %case 1
  \begin{LTR}
    \begin{C++Code}[caption={تابع فلان}]
      int main()
      {
        return 0;
      }
    \end{C++Code}
  \end{LTR}

  %case 2
  \begin{LTR}
    \begin{lstlisting}[style=C++Style,caption=\rl{تابع فلان}]
      int main()
      {
        return 0;
      }
    \end{lstlisting}
  \end{LTR}

\end{document}

Note that I don't have a font on this computer which supports the right script and includes a colon, so the boxes below are actually colons.

captions in RTL

Since this uses caption, it should do all the normal things e.g. get added to the list of listings etc.

Related Question