6

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}[caption=\rl{تابع فلان}]
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.

  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Can the issue be reproduced using fonts from a standard TeX install? If so, you can encourage people to help by adapting your example. If not, a link to some place people can download the fonts would be helpful. – cfr Jan 11 '15 at 3:24
  • I don't think this is a font issue. However, you can find these fonts in fonts-farsiweb and fonts-liberation deb packages. – Eliad Jan 11 '15 at 4:21
  • I'm not saying it is a font issue. But your code cannot be compiled to reproduce the issue as it stands unless people happen to have the fonts. The more things people have to do in order to reproduce the problem, the less likely they are to work on your question and the less likely you are to get helpful answers. You want to make it as easy as possible for people to help. For example, there is no reason to use Liberation Serif rather than a Latin Modern face, which everyone with a standard TeX installation has. If you can't reproduce without the other font, link the upstream source. – cfr Jan 11 '15 at 18:50
  • Can't you just use the default fonts and not specify them at all? – cfr Jan 11 '15 at 18:52
  • The fonts must be specified. I replaced Liberation Serif with Courier. If you do use non-free fonts you can replace Nazli with Arial or something similar. Or you might want to download Free Persian Font. – Eliad Jan 11 '15 at 19:43
4

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.

  • Thanks. This solves half of the problem. The remaining issue is that the lstlistingname, the caption number and the colon appear at the far left. You see the whole caption must be left-to-right oriented. Did you read the P.P.S.? – Eliad Jan 12 '15 at 12:04
  • @Furihr Is this any better? Note that I have no idea what I'm typesetting so I have no intuitive sense of what it should look like. (I can't look at it and 'see' whether things are coming out in the right order because I don't attach any meaning to the different elements of the caption.) – cfr Jan 12 '15 at 13:08
  • There is just one thing. If we use title instead of caption, the entries do not appear in the \lstlistoflistings. Can we do anything about that? Redefine \lstlistoflistings maybe? – Eliad Jan 12 '15 at 15:41
  • Should I mark this as solved and ask a new question for the \lstlistoflistings problem? – Eliad Jan 13 '15 at 17:42
  • @Furihr I don't know. Maybe. I'm not very familiar with the package. Perhaps first try posting a message in chat with a link to the question? – cfr Jan 13 '15 at 21:22

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.