# Highlight differences between character sequences

I have got some character sequences which I need to put into a documentation:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}

\begin{document}

\begin{table}[h]
{\renewcommand\arraystretch{1.25}
\begin{tabular}{|l|l|l|l|} \hline
{} & \multicolumn{2}{l|}{Path} & Probability ($\ln(p)$) \\ \hline
1 & \multicolumn{2}{p{10cm}|}
{\raggedright
10101010101101101010101101010110101010101101011 01010101010110101010101010110101010101010110101 01011010101101010110101101010101010101011010101 01101011101010} &
$-778.850803$ \\\hline
2 & \multicolumn{2}{p{10cm}|}
{\raggedright
10101010101101011010101101010110101010101101011 01010101010110101010101010110101010101010110101 01011010101101010110101101010101010101011010101 01101011101010} &
$-778.854071$ \\\hline
3 & \multicolumn{2}{p{10cm}|}
{\raggedright
10101010101101101010101101010110101010101101011 01010101010110101010101010110101010101010110101 01011010110101010110101101010101010101011010101 01101011101010} &
$-778.875721$ \\\hline
4 & \multicolumn{2}{p{10cm}|}
{\raggedright
10101010101101011010101101010110101010101101011 01010101010110101010101010110101010101010110101 01011010110101010110101101010101010101011010101 01101011101010} &
$-778.878989$ \\\hline
5 & \multicolumn{2}{p{10cm}|}
{\raggedright
10101010101101101010101101010110101010101101011 01010101010110101010101010110101010101010101101 01011010101101010110101101010101010101011010101 01101011101010} &
$-778.965166$ \\\hline
\end{tabular}}
\end{table}

\end{document}

Is there a way to somehow highlight the differences in the second and the other ones compared to the first one? I would like to do it automatically as I have 50 tables like this and some character sequences are longer than this "short" one.

• Welcome to TeX.SE. Do you like to obtain highlight automaticaly or you prepare do this by hand? For example with \textbf{0} or \textbf{1}? Or your is your sequence in verbatim environment? Please, clarify your question. The best would be to wrote small, complete document, which we can copy and compile. May 26, 2016 at 12:36
• Hi. Thanks I have written a small document. I want to make it automatically. It can use an environment whatever makes it possible to do it that way.
– bdvd
May 26, 2016 at 12:53

You can store the first string, then compare each item of the following ones against the corresponding item in the first string.

There's no check that the two strings have the same length, but it may be added.

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{array}

\usepackage{xparse}

\ExplSyntaxOn
\seq_new:N \l_bdvd_comparison_seq
\seq_new:N \l_bdvd_comparison_temp_seq

\NewDocumentCommand{\comparisonstring}{m}
{
\seq_set_split:Nnn \l_bdvd_comparison_seq { } { #1 }
}
\NewDocumentCommand{\usecomparisonstring}{}
{
\seq_use:Nn \l_bdvd_comparison_seq { \allowbreak }
}
\NewDocumentCommand{\comparestring}{m}
{
\seq_set_split:Nnn \l_bdvd_comparison_temp_seq { } { #1 }
\l_bdvd_comparison_seq
\l_bdvd_comparison_temp_seq
\bdvd_compareitems:nn
}
\cs_new_protected:Nn \bdvd_compareitems:nn
{
\str_if_eq:nnTF { #1 } { #2 }
{ #2 }
{ {\fontseries{b}\selectfont #2} }
\allowbreak
}
\ExplSyntaxOff

\begin{document}

\begin{table}[htp]
\renewcommand\arraystretch{1.25}

\comparisonstring{%
10101010101101101010101101010110101010101101011%
01010101010110101010101010110101010101010110101%
01011010101101010110101101010101010101011010101%
01101011101010%
}

\begin{tabular}{|l|>{\raggedright}p{8.5cm}|l|}
\hline
& Path &
\renewcommand\arraystretch{1}
\begin{tabular}{@{}l@{}}Probability \\ ($\ln(p)$) \end{tabular}
\\
\hline
1 & \usecomparisonstring &
$-778.850803$ \\\hline
2 & \comparestring{%
10101010101101011010101101010110101010101101011%
01010101010110101010101010110101010101010110101%
01011010101101010110101101010101010101011010101%
01101011101010} &
$-778.854071$ \\\hline
3 & \comparestring{%
10101010101101101010101101010110101010101101011%
01010101010110101010101010110101010101010110101%
01011010110101010110101101010101010101011010101%
01101011101010} &
$-778.875721$ \\\hline
4 & \comparestring{%
10101010101101011010101101010110101010101101011%
01010101010110101010101010110101010101010110101%
01011010110101010110101101010101010101011010101%
01101011101010} &
$-778.878989$ \\\hline
5 & \comparestring{%
10101010101101101010101101010110101010101101011%
01010101010110101010101010110101010101010101101%
01011010101101010110101101010101010101011010101%
01101011101010} &
$-778.965166$ \\\hline
\end{tabular}
\end{table}

\end{document}

• @bdvd And very slow, too. ;-) May 26, 2016 at 14:07