8

I have this example

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{bidi}

\setmainfont{Amiri}

\parindent=0pt

\begin{document}

{\color{red} text from  left to right  text from  left to right   \par
text from  left to right  text from  left to right }

\hrulefill

\setRTL

\color{red} text from  right to left  text from  right to left
\par  text from  right to left text from  right to left \normalcolor

\hrulefill

{\color{red} text from  right to left  text from  right to left \par
 text from  right to left  text from  right to left}

\hrulefill

{before \color{red} text from  right to left  text from  right to left \par
 text from  right to left  text from  right to left}

\end{document}

With this result enter image description here

Color of text in ltr (left to right) is fine, but in rtl effects of \color is confusing, also the text before \color is colored! look like the command is applied from the beginning to the end of the paragraph without regard of command position.

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{bidi}

\setmainfont{Amiri}

\parindent=0pt

\begin{document}

\setRTL

{\leavevmode\color{red} text from  right to left  text from  right to left \par
text from  right to left}  text from  right to left  \par 
text from  right to left  text from  right to left 

\end{document}

enter image description here

Update 29/06/2018

With \addfontfeature{Color=red} (command from fontspec package) the problem can be solved like that

\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{bidi}

\setmainfont{Amiri}

\parindent=0pt

\begin{document}

\setRTL

before {\addfontfeature{Color=red} text from  right to left  text from  right to left \par
text from  right to left}  text from  right to left  \par 
text from  right to left  text from  right to left 

\end{document}

enter image description here

My aim is to find a method directly with xcolor ( or color) package without reliance on package fontspec.

  • 5
    use \textcolor{red}{....} or \leavevmode\color{red}..... so the colour change is inside the paragraph (there are multiple issues with using \color in vertical mode which are not really solvable without changing the underlying tex mechanisms. – David Carlisle Jun 28 '18 at 7:05
  • @DavidCarlisle Is there a workaround (trick) to solve this (obtain colored text inside group without losing rtl direction of text ) – Salim Bou Jun 28 '18 at 15:58
  • doesn't the suggestion in my first comment work? – David Carlisle Jun 28 '18 at 16:16
  • The text before \color command is also colored, I need onlly text inside braces to be colored. – Salim Bou Jun 28 '18 at 16:29
  • 1
    @SalimBou \addfontfeature{Color=red} not change color of math mode. – mojtaba baghban Jul 5 '18 at 12:32
1

There are solutions, but none of them is nice except the one solution you suggest on your own using fontspec:

% !TeX TS-program = xelatex
\documentclass[12pt]{article}
\usepackage{xcolor}
\usepackage{fontspec}
\usepackage{bidi}

\setmainfont{Amiri}

\parindent=0pt

\newcommand{\parnew}[1][red]{\par\color{#1}}

\begin{document}

{\color{red} text from  left to right  text from  left to right   \par
text from  left to right  text from  left to right }

\hrulefill

\setRTL

{\color{red} text from  right to left  text from  right to left
{\parnew  text from  right to left text from  right to left} \normalcolor

}
\hrulefill

\newcommand{\mycolour}{blue}

{\color{\mycolour}{ text from  right to left  text from  right to left \parnew[\mycolour]
 text from  right to left  text from  right to left}

}
\hrulefill

before {\color{red} text from  right to left  text from  right to left} {\parnew
 text from  right to left  text from  right to left

}
\hrulefill

before \textcolor{red}{ text from  right to left  text from  right to left }\par
\textcolor{red}{text from  right to left  text from  right to left}

\hrulefill

\textcolor{black}{before}   {\color{red}text from  right to left  text from  right to left 

text from  right to left \textcolor{black}{text from  right to left}

}
\hrulefill

before {\addfontfeature{Color=red} text from  right to left  text from  right to left \par
    text from  right to left}  text from  right to left  \par 
text from  right to left  text from  right to left

\hrulefill


\end{document}

enter image description here

  • 1
    The OP explained in his/her comment that an xcolor based solution was needed to patch a class. @mojtababaghban also pointed out \addfontfeature{Color=red} doesn’t change math mode color properly. – Ruixi Zhang Jul 6 '18 at 16:40
  • @RuixiZhang: I read the comment about \addfontfeature{Color=red}. Since it was reported by @mojtababaghban and not the Autor, I thought it might not be a problem. You just need to define new enviromnets such as \begin{myequation}[mycolor]...\end{myequation} or macros as {\myMathmode[mycolor]{...}}. – JoKalliauer Jul 7 '18 at 17:20

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.