4

Regarding the "Arabic Language", words in such language are written such that the letters are connected, unlike English where words are written such that each letter is written separately. However, upon trying to emphasize any letter in Arabic text through the use of any of the options below:

  • Underline: The letter disappear and the word letters are separated
  • Bold: The word letters are separated
  • Italic: The word letters are separated
  • Highlight: The letter disappear and the word letters are separated
  • Frame/Border: Don't know how to do it (Put a single letter in a word in a frame/Border, while maintaining word structure), but I am sure the same problem will arise if I tried placing a single letter in a word into a frame/borderenter image description here

In such cases, the text letters become separated and are no longer connected. Is there a way to emphasize letters through the use of various options while maintaining the word structure where the letters remain attached and are not disconnected?? Here is a MWE where I use Lualatex, notice I used the command described in How to highlight text with an arbitrary color so as to allow custom color regarding highlight command.

\documentclass[a4paper,12pt,twoside]{book}
\usepackage[a4paper,top=2.5cm,bottom=2.5cm,margin=2.5cm,bindingoffset=0.5cm]{geometry} 


\usepackage[bidi=basic,layout=lists.tabular]{babel}
\babelprovide[import=ar,mapdigits,main]{arabic}
\babelprovide[import=en,language=Default]{english}

\babelfont{rm}{Latin Modern Roman}
\babelfont[arabic]{rm}{amiri}
\babelfont[english]{rm}{Times New Roman}

\usepackage{xcolor}
\usepackage{color}

\usepackage{soul}

\definecolor{mygrey}{gray}{0.9}
\sethlcolor{mygrey}

\newcommand{\hlc}[2][yellow]{{%
            \colorlet{foo}{#1}%
            \sethlcolor{foo}\hl{#2}}%
            }

\usepackage[normalem]{ulem}

\begin{document}
\foreignlanguage{english}{This is the word under study}\\
جنوب
\\
\\
\foreignlanguage{english}{Bold}\\
جن
\textbf{و}
ب
\\
\\
\foreignlanguage{english}{underline}\\
جن
\ul{و}
ب
\\
\\
\foreignlanguage{english}{italic}\\
جن
\textit{و}
ب
\\
\\
\foreignlanguage{english}{highlight}\\
جن
\hlc[gray!25]{و}
ب
\\
\\
\end{document}
  • soul package use a font which does not contain Arabic script. – Salim Bou Feb 13 at 20:19
  • @Salim, is there an alternative package?? – Silva Feb 13 at 20:26
  • For entire word a colorbox is enough, but for a single letter we lose connection between letters may be this can be done with lua. – Salim Bou Feb 13 at 20:40
  • 1
    coloring the glyphs should be possible with luacolor or the new color scheme in luaotfload. But everything else interrupts the font shaping. soul is certainly not a good idea. – Ulrike Fischer Feb 13 at 21:42
  • @UlrikeFischer, I can somehow manage to avoid using bold and underlined options but is there a workaround for using frames & borders around letters while maintaining connected letters? – Silva Feb 14 at 2:20
5

Robert's answer with babel, added \arbboxi, \arbboxr, \arbboxl and \arbboxb for isolated, beginning, end, middle form of letters. boxes are drawn with TikZ you can customize them with boxstyle as you like using \tikzset{boxstyle/.append style={...}}

\documentclass[a4paper,12pt,twoside]{book}

\usepackage [bidi=basic,layout=lists.tabular]{babel}
\babelprovide[import=ar,mapdigits,main]{arabic}

\babelfont{rm}{Amiri}

\usepackage{tikz}
\usepackage{ulem}
    
\newcommand{\ulinei}[1]{\uline{#1}}
\newcommand{\ulinel}[1]{^^^^200d\uline{^^^^200d#1}}
\newcommand{\uliner}[1]{\uline{#1^^^^200d}^^^^200d}
\newcommand{\ulineb}[1]{^^^^200d\uline{^^^^200d#1^^^^200d}^^^^200d}

\tikzset{boxstyle/.style={draw=red,inner sep=0pt,fill=yellow}}

\newcommand{\arbboxi}[1]{%
\tikz[baseline=(x.base)]\node(x)[boxstyle]{#1};%
}
\newcommand{\arbboxl}[1]{%
  ^^^^200d\tikz[baseline=(x.base)]\node(x)[boxstyle]{^^^^200d#1};%
}
\newcommand{\arbboxr}[1]{%
  \tikz[baseline=(x.base)]\node(x)[boxstyle]{#1^^^^200d};^^^^200d%
}
\newcommand{\arbboxb}[1]{%
  ^^^^200d\tikz[baseline=(x.base)]\node(x)[boxstyle]{^^^^200d#1^^^^200d};^^^^200d%
}
    
\begin{document}

\Large

جنوب 

\hrulefill

\uliner{ج}نوب
ج\ulineb{ن}وب
جن\ulinel{و}ب
جنو\ulinei{ب}

\hrulefill

\arbboxr{ج}نوب
ج\arbboxb{ن}وب
جن\arbboxl{و}ب
جنو\arbboxi{ب}

\tikzset{boxstyle/.append style={dashed,draw=blue,font=\bfseries,fill=green}}

\hrulefill

\arbboxr{ج}نوب
ج\arbboxb{ن}وب
جن\arbboxl{و}ب
جنو\arbboxi{ب}
    
\end{document}
 

enter image description here

Update 24/02/2020

I have created a command \FormatChar (thanks to @David Carlisle https://tex.stackexchange.com/a/529439/54817) you can apply any commands for a character in Arabic word, the command can also be used with xelatex.

\documentclass[a4paper,12pt,twoside]{book}

\usepackage [bidi=basic,layout=lists.tabular]{babel}
\babelprovide[import=ar,mapdigits,main]{arabic}

\babelfont{rm}{Amiri}

\usepackage{tikz}
\usepackage{ulem}

\def\connecti{i}
\def\connectr{r}
\def\connectl{l}
\def\connectb{b}

\makeatletter
\def\FormatChar#1#2#3#4{%
\let\joineri\@firstofone
\let\joinerii\@firstofone
\def\formatcs{#1}
\def\temp{#3}%
\if\temp\connectb%
\else
 \if\temp\connectr%
 \let\joinerii\@gobble%
 \else
   \if\temp\connectl%
   \let\joineri\@gobble%
   \else    
      \if\temp\connecti%
      \let\joineri\@gobble\let\joinerii\@gobble%
      \else
      \@latex@error
      {\temp is not available for contextual forms}
      \fi%
   \fi%
 \fi%   
\fi%
\xFormatChar{#2}#4}
\def\xFormatChar#1#2{\ifnum#1=1 \joineri{^^^^200d}
\formatcs{\joineri{^^^^200d}#2\joinerii{^^^^200d}}
\joinerii{^^^^200d}\expandafter\@gobbletwo
    \else#2\fi\xFormatChar{\numexpr#1-1\relax}}
\makeatother
    
\tikzset{charboxstyle/.style={draw=red,inner sep=0pt,fill=yellow}}
    
\newcommand{\charbox}[1]{%
\tikz[baseline=(x.base)]\node(x)[charboxstyle]{#1};%
}

\begin{document}

%\FormatChar{#1}{#2}{#3}{#4} 
% #1: Command
% #2: Position of char (number)
% #3: Contextual forms r , l , b , i
% #4: Word
    
\Huge

\FormatChar{\uline}{1}{l}
{جنوب}
%
\FormatChar{\textcolor{blue}}{2}{b}
{جنوب}
%
\FormatChar{\textcolor{red}}{3}{r}
{جنوب}
%
\FormatChar{\charbox}{4}{i}
{جنوب}
%
\tikzset{charboxstyle/.append style={dashed,draw=blue,fill=green,text=orange}}
%
\FormatChar{\charbox}{1}{l}
{جنوب}

\end{document}

enter image description here

Update 28/02/2020

For multiple letters macro \FormatChars can be used (thanks to @egreg https://tex.stackexchange.com/a/529602/54817 )

\documentclass{article}
\usepackage{tikz}
\usepackage{xparse}
\usepackage{ulem}
\usepackage [bidi=basic]{babel}
\babelprovide[import=ar,main]{arabic}

\babelfont{rm}{Amiri}
    
\tikzset{charboxstyle/.style={draw=red,inner sep=0pt,fill=yellow}}

\newcommand{\charbox}[1]{%
\tikz[baseline=(x.base)]\node(x)[charboxstyle]{#1};%
}

\ExplSyntaxOn

\NewDocumentCommand{\FormatChars}{m >{\SplitArgument{1}{-}}m O{i} m}
 {
  \cs_set_nopar:Npn \inputcs { #1 }
  \cs_set_nopar:Npn \joinb {^^^^200d}
  \cs_set_nopar:Npn \joine {^^^^200d}
  \str_case:nnF { #3 }
   {
    { i } { \let\joinb\relax\let\joine\relax }
    { r } { \let\joine\relax }
    { l } { \let\joinb\relax }
    { b } { }
   }
   {
  \msg_error:nn {}{#2~is~not~available~for~contextual~forms,~try~with~r,~l,~or~b} 
   } 
      % #2 is passed as two braced arguments
  \_FormatChars:nnn #2 { #4 }
 }

\cs_new_protected:Nn \_FormatChars:nnn
 {
  % let's analyze the first two args
  \tl_if_novalue:nTF { #2 }
   {% no hyphen in the argument
    \___FormatChars:nnn { #1 } { #1 } { #3 }
   }
   {
    \bool_lazy_or:nnTF { \tl_if_blank_p:n { #1 } } { \tl_if_blank_p:n { #2 } }
     {% argument is -n or m- or -
      \tl_if_blank:nTF { #1 }
       {
        \tl_if_blank:nTF { #2 }
         {% argument is -
          \joinb\inputcs{\joinb #3\joine}\joine
         }
         {% argument is -n
          \___FormatChars:nnn { 1 } { #2 } { #3 }
         }
       }
       {% argument is m-
        \___FormatChars:nnn { #1 } { -1 } { #3 }
       }
     }
     {% argument is m-n
      \___FormatChars:nnn { #1 } { #2 } { #3 }
     }
   }
 }

\cs_new_protected:Nn \___FormatChars:nnn
 {
  \int_compare:nTF { #1 > #2 > 0 }
   {
    #3
   }
   {
    \tl_range:nnn { #3 } { 1 } { #1 - 1 }
    \joinb\inputcs{\joinb \tl_range:nnn { #3 } { #1 } { #2 }\joine }\joine
    \tl_range:nnn { #3 } { #2 + 1 } { -1 }
   }
 }

\ExplSyntaxOff


\begin{document}

%\FormatChars{#1}{#2}[#3]{#4} 

% #1  : command to apply on range ex : \uline 
% #2  : range of letters ex : 2-6
% #3  : type of connection : i , r , l , b (default i)
% #4  : word    

\Huge

\FormatChars{\uline}{1-3}
{جنوب}
%
\FormatChars{\textcolor{blue}}{2-3}[r]
{جنوب}
%
\FormatChars{\textcolor{red}}{2-}[r]
{جنوب}
%
\FormatChars{\charbox}{4}
{جنوب}
%
\tikzset{charboxstyle/.append style={draw=blue,fill=black,text=white}}
%
\FormatChars{\charbox}{-2}[l]
{جنوب}

\end{document}

enter image description here

| improve this answer | |
  • Very nice, is there a way to also add the bold option, make bold letters while maintaining connection between letters??? In addition, regarding the frames, can we add an option to the command to control whether the frame is dashed, continued... in addition to the line thickness? – Silva Feb 19 at 21:30
  • Answer updated you can change or add more options for \arbbox style with \tikzset{boxstyle/.append style={...}} – Salim Bou Feb 20 at 6:16
  • OK, regarding the bold option, can it be used while maintaining connection between letters??? – Silva Feb 20 at 9:33
  • Yes with \tikzset{boxstyle/.append style={draw=none,fill=none,font=\bfseries}} but difference between letters appears at connections in one side a thick letter and in the other a thin. – Salim Bou Feb 20 at 9:43
8

The following can be improved in various ways (e.g. using tikz is probably a bit overkill and also doesn't work well in RTL mode; there should be a way to select the glyphs which gets a frame and to change the colors; glue and other nodes are currently ignored, so it works only for single words) but it shows that it is basically possible to inspect the words, get the dimensions and draw something which doesn't interfere with the font shaping:

\documentclass[a4paper,12pt,twoside]{book}

\usepackage [bidi=basic,layout=lists.tabular]{babel}
\babelprovide[import=ar,mapdigits,main]{arabic}
\babelprovide[import=en,language=Default]{english}

\babelfont{rm}{Latin Modern Roman}
\babelfont[arabic]{rm}[Renderer=HarfBuzz]{amiri}
\babelfont[english]{rm}{Times New Roman}


\usepackage{tikz}

\newsavebox\myword

\usepackage{luacode}
\begin{luacode}
function frameword (boxnum)
   local head = tex.box[boxnum].head
   local colortable={"yellow","red","blue","green"}
   local GLYPH           = node.id("glyph")
   local widthtable={}
   local heighttable={}
   local depthtable={}
   for n in node.traverse(head) do
    local id = n.id
    if id == GLYPH then
      table.insert(widthtable,n.width);
      table.insert(heighttable,n.height);
      table.insert(depthtable,n.depth);
      texio.write_nl (" WIDTH ".. n.width .. " CHAR " .. n.char)
    else
      texio.write_nl ("????")
    end
   end
    for i = table.getn(widthtable), 1, -1 do
     color = colortable[math.fmod(i,4)+1]
     tex.sprint("\\printrectangle{"..color.."}{".. widthtable[i] .."}{" .. heighttable[i].."}{" .. depthtable[i] .."}")
    end
end
\end{luacode}
\begin{document}
جنوب 



\newcommand\printrectangle[4]{\tikz[overlay]\draw[#1,opacity=0.5](0,-#4sp)rectangle(#2sp,#3sp);\hspace{#2sp}}
\savebox\myword{\textdir TRT جنوب} 



\leavevmode\foreignlanguage{english}{\directlua{frameword(\the\myword)}\llap{\usebox\myword}}

\end{document}

enter image description here

| improve this answer | |
  • you must notice that the option [Renderer=HarfBuzz] in the amiri font caused the letters to become seperated so I had to remove it – Silva Feb 19 at 20:43
  • @Silva My screenshots show what I get. But you need to use lualatex-dev (and a current texlive which has luahbtex as engine) to make use of the Harfbuzz library. – Ulrike Fischer Feb 19 at 20:46
  • lualatex-dev is still beta right, is it safe to use? Anyway, if it is not of any annoyance to you (I know you're a bit busy and also mentioned that "The following can be improved in various ways", so you expect other users to further enhance such solution), the code is a bit too much for me, can you provide any hints regarding setting a command with optional arguments to set which letter in the word to be framed, set frame color, line thickness, line type (dashed, continous), as well as backgrond color (Highlight), since the command it not defined in the preamble and is defined mid-document. – Silva Feb 19 at 21:59
  • no lualatex-dev is not beta. Currently lualatex and lualatex-dev use the same latex, only that lualatex-dev uses the luahbtex engine. It is fine and it is much better with scripts like arabic than the luatex engine. And no, I don't have the time to expand the code. I already spent too much time on it. – Ulrike Fischer Feb 19 at 22:02
4

EDIT: I added below a rather primitive approach for drawing frames. Then, in the document itself, I added the invisible ^^^^200d mark at the appropriate places.

Not an answer, but only a suggestion. In my opinion, coloring the glyphs is the way to go in Arabic typographically speaking. Underlining may also be considered.

\documentclass[12pt]{article}

\usepackage[novoc]{arabluatex}

\newcommand{\ulinel}[1]{^^^^200d\uline{^^^^200d#1}}
\newcommand{\uliner}[1]{\uline{#1^^^^200d}^^^^200d}
\newcommand{\ulineb}[1]{^^^^200d\uline{^^^^200d#1^^^^200d}^^^^200d}

\NewDocumentCommand{\arbbox}{O{white} O{white} m}{%
  \fboxsep=0pt%
  \fcolorbox{#1}{#2}{#3}%
}

\begin{document}

\begin{arab}
  ^ganUb
  ^gan\arbcolor[blue]{U}b
  ^gan\ulinel{U}b
  ^ga\ulineb{n}Ub
  ^gan‍\arbbox[red][yellow]{‍U}b
\end{arab}

\end{document}

enter image description here

| improve this answer | |
  • Is there a way to do this while using Unicode fonts instead of arabic transliterations? I do not have the opportunity to use arabic transliterations. – Silva Feb 19 at 19:10
  • @Silva For the record, arabluatex also provides \txarb{} and \begin{txarab}...\end{txarab} for inserting Unicode Arabic. That said, the babel way is excellent. – Robert Alessi Feb 20 at 5:37
  • So I can use the exact above code with babel? – Silva Feb 20 at 9:26
  • Also, regarding the bold option, can it be used while maintaining connection between letters??? – Silva Feb 20 at 9:33
  • @Silva Yes, with babel, just remove arabluatex and its Arabic environments, and type away in Arabic. xparse is required for building commands with \NewDocumentCommand and luacolor for coloring Arabic glyphs as said by Ulrike. You may pattern your commands after the above \ulineX and relplace uline with textbf and it will work. – Robert Alessi Feb 20 at 14:38
4

With luacolor you can color the glyphs too

\documentclass[a4paper,12pt,twoside]{book}
\usepackage[a4paper,top=2.5cm,bottom=2.5cm,margin=2.5cm,bindingoffset=0.5cm]{geometry}


\usepackage[bidi=basic,layout=lists.tabular]{babel}
\babelprovide[import=ar,mapdigits,main]{arabic}
\babelprovide[import=en,language=Default]{english}

\babelfont{rm}{Latin Modern Roman}
\babelfont[arabic]{rm}{amiri}
\babelfont[english]{rm}{Times New Roman}

\usepackage{xcolor}
\usepackage{luacolor}
\begin{document}
\foreignlanguage{english}{This is the word under study}\\
جنوب\\
\color{yellow}ج\color{red}ن\color{blue}و\color{green}ب 
\end{document}

enter image description here

| improve this answer | |
  • ,Ok, what about highlighting them, can this be done? Also how is the package (Luaotfload) used? – Silva Feb 19 at 20:29
  • see my other answer. The printrectangle could have a background instead of only a frame. – Ulrike Fischer Feb 19 at 20:30

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.