10

I'm creating some tables with conjugations for nouns, adjectives, etc., for certain languages. I wanted to colour certain endings of these instances but doing it manually is very tedious, so I tried to look for a way to automatise it. I have searched on high and low and asked in chat, but I could not find anything.

I could have used the listing packages, but the strings are regular text, not code. Furthermore, there are some obstacles that make this a bit harder to achieve:

  • The alphabet in this case is not the Latin one, but Russian.

  • Although it's only the endings of words, not the whole word, some endings match with single words. For example, моего is the genitive for мой (mine), but there is also его (his). I don't want this last one to be coloured.

    As an example, if colour = bold, then they should appear as "моего" and "его" respectively. Below I'll provide a very minimal example of what I want. If possible, I'd like to do this without using the command in the table (although I seriously doubt it).

Is this doable? Here's my desired result and a very minimal example. In this case there is one ending and one colour, but I'd like to use this with multiple colours and also multiple endings.

table figure

\documentclass[margin=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{xcolor}

\begin{document}
\begin{tabular}{|c|c|}\hline
  мой & мо\textcolor{blue}{его} \\
  твой & тво\textcolor{blue}{его} \\
  его/её & его/её \\
  наш & наш\textcolor{blue}{его} \\
  ваш & ваш\textcolor{blue}{его} \\
  их & их \\ \hline
\end{tabular}
\end{document}
3
  • Probably using a specialized scripting language is a better idea.
    – egreg
    Commented May 5, 2015 at 15:28
  • 1
    Perhaps \def\ego{\textcolor{blue}{его}} so that your entries would be мо\ego, тво\ego, наш\ego, etc.?? Commented May 5, 2015 at 15:30
  • @StevenB.Segletes Interesting. Thanks for the suggestion! I'll wait for more solutions just in case, but yours looks good.
    – Alenanno
    Commented May 5, 2015 at 15:37

4 Answers 4

7

I'd mark the suffix manually but let tex choose the colour from a pre-determined list:

enter image description here

\documentclass[margin=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{xcolor}

\def\>#1 {\csname suffix\detokenize{#1}\endcsname{#1} }

\expandafter\def\csname suffix\detokenize{его}\endcsname{\textcolor{blue}}
\expandafter\def\csname suffix\detokenize{х}\endcsname{\textcolor{red}}
\expandafter\def\csname suffix\detokenize{её}\endcsname{\textcolor{green}}

\begin{document}
\begin{tabular}{|c|c|}\hline
  мой & мо\>его \\
  твой & тво\>его \\
  его/её & его/\>её \\
  наш & наш\>его \\
  ваш & ваш\>его \\
  их & и\>х \\ \hline
\end{tabular}
\end{document}

Note the space after the word is required here. (That could be avoided if necessary)

9
  • You mean the space after the words in the columns? It's fine. I always leave one space in my tables. Thank you. The "её" and "их" could be left black but I appreciate the effort. :D
    – Alenanno
    Commented May 5, 2015 at 15:51
  • One question: if I want to apply the same colour to two different endings, would that require me to duplicate one of those commands in the preamble? If the alternative is much harder, then don't worry. I'm just asking to be sure.
    – Alenanno
    Commented May 5, 2015 at 15:53
  • yes to space in the column, I guessed they probably were not the other suffixes you had in mind, but they were there and I wanted to show more than one colour:-) Commented May 5, 2015 at 15:53
  • @Alenanno that would be the easiest way yes, the definition can be any command that takes an argument, so if you want его and abc to be same colour but only give the colour once you could do as above and then \expandafter\def\csname suffix\detokenize{abc}\endcsname{\csname suffix\detokenize{его}\endcsname} and abc will get whatever colour you give его Commented May 5, 2015 at 15:57
  • 1
    @Alenanno that's easier really just have \def\>#1 {\textcolor{mycolor}{#1} } then \definecolor{mycolor}{...} to define mycolor to whatever you want before each row of tables. Commented May 6, 2015 at 13:28
4

In accordance with your new example I can show you how to set this in TeX.

\input cyrchars
\input opmac

\def\t#1[#2,#3]#4// {{\rulewidth=.8pt \localcolor#1\def\tmpb{#4}%
   \ifx^#2^\else \replacestrings{ #2 }{ {#2} }\replacestrings{#2 }{{#1#2}}\fi
   \ifx^#3^\else \replacestrings{ #3 }{ {#3} }\replacestrings{#3 }{{#1#3}}\fi
   \frame{\vbox{\Black\expandafter\tA\tmpb//}}}%
   \kern5pt
}
\def\tA #1/{\if/#1/\else \hbox to4em{\strut\hss#1\hss}\expandafter\tA\fi}

\hbox{%
\t \Blue[,]    мой / твой / его / её  / наш / ваш / их //   
\t \Blue[его,] моего / твоего / его / её / нашего / вашего / их //
\t \Blue[ему,] моему / твоему / его / её / нашему / вашему / их //
\t \Blue[его,] моего / твоего / его / её / нашего / вашего / их //
\t \Blue[,]    мой / твой / его / её  / наш / ваш / их //     
\t \Blue[им,]  моим / твоим / его / её / нашим / вашим / их //
\t \Blue[ём,]  моём / твоём / его / её / нашём / вашём / их //
}\kern5pt
\hbox{%
\t \Red[я,a]  моя / твоя / его / её  / нашa / вашa / их //   
\t \Red[ей,]  моей / твоей / его / её / нашей / вашей / их //
\t \Red[ей,]  моей / твоей / его / её / нашей / вашей / их //
\t \Red[ю,у]  мою  / твою / его / её  / нашу / вашу / их //  
\t \Red[ю,у]  мою  / твою / его / её  / нашу / вашу / их //  
\t \Red[ей,]  моей / твоей / его / её / нашей / вашей / их //
\t \Red[ей,]  моей / твоей / его / её / нашей / вашей / их //
}\kern5pt
\hbox{%
\t \Green[ё,е]   моё / твоё / его / е{ё}  / наше / ваше / их //     
\t \Green[его,]  моего / твоего / его / её / нашего / вашего / их //
\t \Green[ему,]  моему / твоему / его / её / нашему / вашему / их //
\t \Green[ё,е]   моё / твоё / его / е{ё}  / наше / ваше / их // 
\t \Green[ё,е]   моё / твоё / его / е{ё}  / наше / ваше / их // 
\t \Green[им,]   моим / твоим / его / её / нашим / вашим / их //
\t \Green[ём,ем] моём / твоём / его / её  / нашем / вашем / их //
}   
\bye

Use pdfcsplain to process this, because \input cyrchars uses csplain's UTF8 feature. The result:

moj

The main idea is to store the converted string and use conversion process using \replacestrings (LaTeX has something similar, I hope). The conversion process does the following:

\replacestrings{ его }{ {его} }% no conversions, if the suffix=word.
\replacestrings{его }{{\Blue его} }% do conversion
4
  • Thanks, looks nice, but this is not usable in LaTeX, correct?
    – Alenanno
    Commented May 5, 2015 at 16:38
  • @Alenanno Ideas are usable in LaTeX, but implementation differ.
    – wipet
    Commented May 5, 2015 at 16:43
  • Yeah. That's what I meant (and what I was fearing). :P
    – Alenanno
    Commented May 5, 2015 at 16:44
  • Ahah awesome! +1 :)
    – Alenanno
    Commented May 6, 2015 at 13:37
3

First, let me say that I am no expert in the fine points of xparse.sty. That said, it occurred to me that something like this might prove useful:

\documentclass[margin=10pt]{standalone} 
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{collcell}

%% This is a two-part process. collcell.sty wraps the contents of
%% the cell in braces {...}. The first step is to remove the braces.
%% The word in the cell is then split into two arguments at the '.',
%% the second argument possibly being empty (no '.').
%% Change the {.} below to a {*} or a {-} or whatever you like.
%% Note that the default color, blue, is set here.
\DeclareDocumentCommand{\foo}{m}{\fooa#1\stop}
\DeclareDocumentCommand{\fooa}
    {O{blue}>{\SplitArgument{1}{.}}u{\stop}}
    {\coloritt[#1]#2}

%% If there is no '.' just print the word; otherwise print
%% the first half in black and the second half in blue.
\DeclareDocumentCommand\coloritt{omm}{%
     \IfNoValueTF{#3}{#2}{#2\textcolor{#1}{#3}}%
}

%% This is necessary to be able to use the contents of the
%% cell as an argument to \foo.
\newcolumntype{C}{>{\collectcell\foo}c<{\endcollectcell}} 

\begin{document}

\begin{tabular}{|c|C|}\hline
   мой & мо.его \\
   твой &[red] тво.его \\ %% <<-- Alternate color
   его/её & его/её \\
   наш & наш.его \\
   ваш & ваш.его \\
   их & их \\ \hline
 \end{tabular}

\end{document}

Which produces this:

New example of the table

Just add a period in the word where you want the colored text to start. I used a period -- you could use an asterisk or even a hyphen.

Later

As I said, I am no expert in this, but Joseph Wright is. Thanks to his answer to my question, I have altered the code to make available other colors for use. Just add [<color>] to the cell -- there is an example in the code.

2

Pursuing further sgmoye's idea, here's an implementation with l3regex that's easily extendable to several color change markers.

\documentclass[margin=10pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[russian]{babel}
\usepackage{xcolor}
\usepackage{xparse,l3regex}
\usepackage{collcell}

\newcolumntype{C}{>{\collectcell\colorterm}c<{\endcollectcell}}

\ExplSyntaxOn
\NewDocumentCommand{\colorterm}{m}
 {
  \alenanno_colorterm:n { #1 }
 }

\tl_new:N \l_alenanno_colorterm_word_tl

% add here the tokens that mark the color change
% for the moment . and / are set
\regex_const:Nn \c_alenanno_markers_regex { ([./]) (.*) }

\cs_new_protected:Nn \alenanno_colorterm:n
 {
  \tl_set:Nn \l_alenanno_colorterm_word_tl { #1 }
  \regex_replace_once:NnN \c_alenanno_markers_regex
   { \c{alenanno_colorize:nn}\cB\{\1\cE\}\cB\{\2\cE\} }
   \l_alenanno_colorterm_word_tl
  \tl_use:N \l_alenanno_colorterm_word_tl
 }

\cs_new:Nn \alenanno_colorize:nn
 {
  \textcolor{alenanno#1}{#2}
 }
\ExplSyntaxOff

% any token denoting a color change should have
% a corresponding color (name prefixed by alenanno)
\definecolor{alenanno.}{rgb}{1,0,0}
\definecolor{alenanno/}{rgb}{0,0,1}

\begin{document}

\begin{tabular}{|c|C|}\hline
  мой & мо.его \\
  твой & тво.его \\
  его/её & его/её \\
  наш & наш.его \\
  ваш & ваш.его \\
  их & их \\ \hline
\end{tabular}
\end{document}

The \colorterm command can also be used in running text as \colorterm{мо.его}, with the same effect.

enter image description here

Would you want to add another marker, say ?, add it to the list

\regex_const:Nn \c_alenanno_markers_regex { ([./?]) (.*) }

and also do

\definecolor{alenanno?}{rgb}{x,y,z}

(or using any color definition command among those of xcolor).

Explanation. The input мо.его is transformed into

мо\alenanno_colorize:nn{.}{его}

and \alenanno_colorize:nn{#1}{#2} is syntactic sugar for \textcolor{alenanno#1}{#2}.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .