7

With luatex there is \textdir to change text direction and tex.textdir which give string representing text direction.

I have created this macro to test text direction which supposedly gives TLT inside left to right context and TRT inside right to left context, but my macro does not give the desired result

\documentclass{article}
\usepackage{fontspec}
\setmainfont[Script=Arabic]{Amiri}

\edef\dir{TLT}
\def\testdir{\directlua{tex.print(tex.textdir)}}
\def\foo{\ifx\dir\testdir TLT \else TRT \fi}

\begin{document}

%\textdir TRT

\foo ; \directlua{tex.print(tex.textdir)}


\end{document}
  • 2
    \ifx does no expansion and only looks at the first level expansion when comparing two macros. – egreg Apr 2 at 10:49
3

\ifx only looks at the first level expansion when comparing two control sequences and cannot compare strings.

\documentclass{article}
\usepackage{fontspec}
\setmainfont[Script=Arabic]{Amiri}

\ExplSyntaxOn
\NewExpandableDocumentCommand{\ifdirTLT}{mm}
 {
  \str_if_eq:eeTF { TLT } { \lua_now:n { tex.print(tex.textdir) } }
   { #1 }
   { #2 }
 }
\ExplSyntaxOff

\begin{document}

\textdir TLT

\ifdirTLT{TLT}{TRT}; \directlua{tex.print(tex.textdir)}

\textdir TRT

\ifdirTLT{TLT}{TRT}; \directlua{tex.print(tex.textdir)}

\end{document}

enter image description here

3

You need to expand the \directlua before the test. E.g.

\documentclass{article}
\usepackage{fontspec}
\setmainfont[Script=Arabic]{Amiri}

\ExplSyntaxOn
\cs_generate_variant:Nn\tl_if_eq:nnTF {xn}
\def\foo{\tl_if_eq:xnTF{\directlua{tex.print(tex.textdir)}}{TLT}{TLT}{TRT}}
\ExplSyntaxOff

\begin{document}

%

\foo ; \directlua{tex.print(tex.textdir)}

\textdir TRT
\foo ; \directlua{tex.print(tex.textdir)}

\end{document}

enter image description here

  • Is there an equivalent macro without latex3? – Salim Bou Apr 2 at 10:55
  • You can at first expand the command with \edef\tmpa{\directlua ...} and then use \ifx\tmpa\dir but expl3 solutions are imho better (and egreg's test is better than mine, comparing this as string is cleaner.) – Ulrike Fischer Apr 2 at 12:42
  • The expansion should come just before the test like this \def\foo{\edef\testdir{\directlua{tex.print(tex.textdir)}} \ifx\testdir\dir TLT \else TRT \fi} For the LaTeX3 syntax I find it a bit difficult. – Salim Bou Apr 2 at 13:22
  • @SalimBou Just do the comparison in Lua. \directlua{tex.print(tex.textdir == "TLT" and "TLT" or "TRT")} – Henri Menke Apr 3 at 5:50
  • @HenriMenke I would like to create \if conditional which test the text direction to change some tex dimensions not just to print TLT or TRT like this \if<text direction is TLT> do this \else do that \fi – Salim Bou Apr 3 at 8:36

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.