6
$\begingroup$

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?

$\endgroup$
7
$\begingroup$

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"]

screenshot

$\endgroup$
  • $\begingroup$ ShowAutoStyles -> True is not a sufficient condition for syntax highlighting. The cell also must be an input cell and be displayed in standard form. $\endgroup$ – m_goldberg Aug 24 '15 at 9:47
  • $\begingroup$ @m_goldberg I updated the answer and removed previous hasty conclusions. $\endgroup$ – Alexey Popkov Apr 29 '16 at 14:57
  • $\begingroup$ 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. $\endgroup$ – Berg Jun 10 '16 at 15:15
  • $\begingroup$ @Berg I can't test right now but try to add the option Magnification -> Inherited at the end. $\endgroup$ – Alexey Popkov Jun 10 '16 at 15:47
3
$\begingroup$

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:

enter image description here

$\endgroup$
2
$\begingroup$

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.

$\endgroup$

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.