8
$\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$
1

3 Answers 3

9
$\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$
4
  • $\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
    Commented Aug 24, 2015 at 9:47
  • $\begingroup$ @m_goldberg I updated the answer and removed previous hasty conclusions. $\endgroup$ Commented Apr 29, 2016 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
    Commented Jun 10, 2016 at 15:15
  • $\begingroup$ @Berg I can't test right now but try to add the option Magnification -> Inherited at the end. $\endgroup$ Commented Jun 10, 2016 at 15:47
5
$\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 and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.