# Syntax highlighting in text cells

I want to write some documentation for a notebook using text cells. Now, I can select parts of the text within such a text cell and set its style to "Input". This does change its font, but there is no syntax highlighting (neither the right color nor are patterns slanted). Is it possible to add styles and syntax coloring to inline cells so that they look like input cells?

According to this answer, there is a way to make an inline cell with automatic syntax highlighting. For example we can create a "Text" cell containing an inline "Input" cell with automatic syntax highlighting:

CellPrint@TextCell[
Row[{"This is an inline cell with automatic syntax highlighting: ",
ExpressionCell[Defer@Plot[x, {x, 0, 1}, PlotLabel -> "string"], "Notebook", "Input",
CellFrame -> True]}], "Text"]


• ShowAutoStyles -> True is not a sufficient condition for syntax highlighting. The cell also must be an input cell and be displayed in standard form. – m_goldberg Aug 24 '15 at 9:47
• @m_goldberg I updated the answer and removed previous hasty conclusions. – Alexey Popkov Apr 29 '16 at 14:57
• Thanks, this helps a bit. Unfortunately, the input subcell is incompatible with Magnification, i.e. it is very small on my 4K-monitor. Removing the "Notebook" option fixes this but removes the syntax highlighting. – Berg Jun 10 '16 at 15:15
• @Berg I can't test right now but try to add the option Magnification -> Inherited at the end. – Alexey Popkov Jun 10 '16 at 15:47

You can set the DefaultInlineFormatType option of Text cells to "StandardForm", and then set the LanguageCategory and ShowAutoStyles options of "InlineCell" to achieve what you want. For example:

styles = {
Cell[StyleData["InlineCell"],
ShowAutoStyles->True,
LanguageCategory->"Input",
FontWeight->"DemiBold",
FontFamily->"Source Sans Pro"
],
Cell[StyleData["Text"],
DefaultInlineFormatType->"StandardForm"
]
};

SetOptions[
EvaluationNotebook[],
StyleDefinitions -> Replace[CurrentValue[EvaluationNotebook[], StyleDefinitions],
{
Notebook[oldcells_, r__] :> Notebook[Join[oldcells, styles], r],
other_ :> Notebook[Prepend[styles, Cell[StyleData[StyleDefinitions->other]]], StyleDefinitions -> "PrivateStylesheetFormatting.nb"]
}
]
]


Input cells are in StandardForm, and use the DemiBold version of the "Source Sans Pro" font. Syntax coloring occurs when both ShowAutoStyles and ShowSyntaxStyles are true, and the LanguageCategory is "Input". I don't need to specify the ShowSyntaxStyles option because it is true by default for StandardForm. Here is an example text cell with this stylesheet:

I don't think so. Syntax highlighting is a property peculiar to input cells and doesn't pertain to a StyleBox, which is what your get when you apply the style Input to a selection made in a text cell.