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.
Since this uses caption
, it should do all the normal things e.g. get added to the list of listings etc.
The macro \citeA
is "fragile", in the LaTeX-specific sense of the word. If it occurs inside a "moving argument" (more LaTeX jargon, sorry), such as the argument of a \caption
instruction, one must prefix a \protect
instruction:
\caption{\label{fig:researchscope}The process of concept-driven
design approach in relation to theory and use situation,
adopted from \protect\citeA[p.~282]{rub14}}
For another example of this type, see the posting Using \input{} inside caption.
Best Answer
It seems a typical "fragile command in a moving argument problem". However the following document doesn't show any problem:
So I suspect that you're using some package that kill the robustness of
\cite
in some way.A way to solve the issue is writing
in the caption, but probably it can be better addressed with a minimal example available.
As correctly remarks Gonzalo Medina in his comment, there's also the possibility of writing
so that only "Example of something" will be sent to the list of listings (which is very important if the adopted bibliography style is "unsorted").