Index: /cpp/frams/genetics/genman.cpp
===================================================================
--- /cpp/frams/genetics/genman.cpp	(revision 964)
+++ /cpp/frams/genetics/genman.cpp	(revision 965)
@@ -86,5 +86,5 @@
 static ParamEntry GMparam_tab[] =
 {
-	{ "Genetics", 1, 10, "GenMan", },
+	{ "Genetics", 1, 11, "GenMan", },
 	{ "gen_hist", 0, PARAM_DONTSAVE, "Remember history of genetic operations", "d 0 1 0", FIELD(history), "Required for phylogenetic analysis", },
 	{ "gen_hilite", 0, 0, "Use syntax highlighting", "d 0 1 1", FIELD(hilite), "Use colors for genes?\n(slows down viewing/editing of huge genotypes)", },
@@ -92,5 +92,6 @@
 	{ "operReport", 0, PARAM_DONTSAVE, "Operators report", "p()", PROCEDURE(p_report), "Show available genetic operators", },
 	{ "toHTML", 0, PARAM_DONTSAVE, "HTMLize a genotype", "p s(s)", PROCEDURE(p_htmlize), "returns genotype expressed as colored HTML", },
-	{ "toHTMLshort", 0, PARAM_DONTSAVE, "HTMLize a genotype, shorten if needed", "p s(s)", PROCEDURE(p_htmlizeshort), "returns genotype (abbreviated if needed) expressed as colored HTML", },
+	{ "toHTMLshort", 0, PARAM_DONTSAVE, "HTMLize a genotype, shorten if needed", "p s(s)", PROCEDURE(p_htmlizeshort), "returns genotype (abbreviated if needed) in colored HTML format", },
+	{ "toLaTeX", 0, PARAM_DONTSAVE, "LaTeXize a genotype", "p s(s)", PROCEDURE(p_latexize), "returns genotype in colored LaTeX format", },
 	{ "validate", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Validate", "p oGeno(oGeno)", PROCEDURE(p_validate), "returns validated (if possible) Geno object from supplied Geno", },
 	{ "mutate", 0, PARAM_DONTSAVE | PARAM_USERHIDDEN, "Mutate", "p oGeno(oGeno)", PROCEDURE(p_mutate), "returns mutated Geno object from supplied Geno", },
@@ -523,4 +524,63 @@
 }
 
+string GenMan::LaTeXize(const char *g)
+{
+	char buf[50];
+	int len = strlen(g);
+	int chars = 0, lines = 0; //currently not used
+	uint32_t *styletab = new uint32_t[len];
+	getFullStyle(g, styletab);
+	string latex = "\\usepackage{xcolor}\n% Using \\texttt{} may be beneficial for some genetic encodings, but then you may lose bold/italic.\n\\noindent \\sloppy";
+	uint32_t prevstyle, prevcolor, style = 0, color = 0;
+	for (int i = 0; i < len; i++)
+	{
+		if (g[i] == '\r') continue;
+		if (g[i] == '\n') { latex += "\\\\\n"; lines++; continue; }
+		chars++;
+		prevstyle = style;
+		prevcolor = color;
+		style = GENGETSTYLE(styletab[i]);
+		color = GENGETCOLOR(styletab[i]);
+
+		// Unfortunately, LaTeX (as opposed to HTML) uses the same closing tags "}" for color, bold, italic, underline - so they cannot intersect.
+		// Therefore we have to "turn off" everything on every change of style or color, and then "turn on" (to avoid problems with e.g. red-bold-blue-unbold or bold-italic-unbold-unitalic).
+		// This could be optimized by a more complex logic and tracking which color/style section starts and ends within another section.
+
+		if (((style & GENSTYLE_INVALID) != (prevstyle & GENSTYLE_INVALID))
+			||
+			((style & GENSTYLE_BOLD) != (prevstyle & GENSTYLE_BOLD))
+			||
+			((style & GENSTYLE_ITALIC) != (prevstyle & GENSTYLE_ITALIC))
+			||
+			((i == 0 || (color != prevcolor))))
+		{
+			if (prevstyle & GENSTYLE_INVALID) latex += "}";
+			if (prevstyle & GENSTYLE_BOLD) latex += "}";
+			if (prevstyle & GENSTYLE_ITALIC) latex += "}";
+			if (i != 0) latex += "}"; //for color
+			if (style & GENSTYLE_INVALID) latex += "\\underline{";
+			if (style & GENSTYLE_BOLD) latex += "\\textbf{";
+			if (style & GENSTYLE_ITALIC) latex += "\\textit{";
+			sprintf(buf, "\\textcolor[rgb]{%.2g,%.2g,%.2g}{", GENGET_R(color) / 255.0, GENGET_G(color) / 255.0, GENGET_B(color) / 255.0); latex += buf;
+		}
+		if (g[i] == '<') latex += "$<$"; else if (g[i] == '>') latex += "$>$"; else
+			if (g[i] == '-') latex += "$-$"; else if (g[i] == '|') latex += "$|$"; else
+				if (g[i] == '$') latex += "\\$"; else if (g[i] == '%') latex += "\\%"; else latex += g[i];
+		if ((i % 3) == 0 && g[i] == ' ') latex += "\n"; //for readability, insert some newlines into latex...
+		if (i % 10 == 0) latex += "{\\hskip 0pt}"; // https://tex.stackexchange.com/questions/33526/automatic-line-breaking-of-long-lines-of-text
+	}
+	delete[] styletab;
+	latex += "}"; //for color (it was used at least once)
+	if (style & GENSTYLE_BOLD) latex += "}";
+	if (style & GENSTYLE_ITALIC) latex += "}";
+	latex += "\n";
+	return latex;
+}
+
+void GenMan::p_latexize(ExtValue *args, ExtValue *ret)
+{
+	ret->setString(LaTeXize(args->getString().c_str()).c_str());
+}
+
 Geno GenMan::getSimplest(const SString& format)
 {
Index: /cpp/frams/genetics/genman.h
===================================================================
--- /cpp/frams/genetics/genman.h	(revision 964)
+++ /cpp/frams/genetics/genman.h	(revision 965)
@@ -60,4 +60,5 @@
 	GenoOperators* getOper_f(const SString& format);
 	string HTMLize(const char *g, bool shorten);
+	string LaTeXize(const char *g);
 	int findOperFormatIndex(const SString& format);
 public:
@@ -80,4 +81,5 @@
 	PARAMPROCDEF(p_htmlize);
 	PARAMPROCDEF(p_htmlizeshort);
+	PARAMPROCDEF(p_latexize);
 	PARAMPROCDEF(p_validate);
 	PARAMPROCDEF(p_mutate);
