6

To show the difference between my theoretical an practical part, I want to change the background color of the page during practical sections/parts.

Until now I used a new paragraph style, which worked fine: create new paragraph style

Because I can not use Headings or anything like that, I had to use a shaded box. This also worked fine, but I can not insert numbered illustrations and I get the following error:

> ! LaTeX Error: Not in outer par
> mode.
>  \begin{figure}[h]
>                         You've lost some text.  Try typing  <return>  to proceed. If that doesn't work, type  X <return>  to quit.

Does anybody know how to manage to change the background color for individual sections (including headings, images, ...) in LyX?

2 Answers2

3

Assumption: The background color is changed only on new pages!

Under "Document Settings > Local Layout" put

Style PageColor
    Category    MainText        
    LatexType   Command
    LatexName   pagecolor
    LabelType   Static
    LabelString "Page Color:"
    LabelSep    xx
    LabelFont
      Color     Red
      Shape     Italic
    EndFont
    Align                 Center
    Preamble
      \usepackage[dvipsnames]{xcolor}
    EndPreamble
End

and "validate". Then after a section title, choose the style "PageColor" and write the name of a color.

lyx document

Notes:

  1. "PageColor" will set the background color for the entire page. If several "PageColor" occur on the same page only the last one has effect. That is the reason of our assumption.

  2. For the colors, all color names of the package "xcolor" are supported. Using the above setting all the colors via the dvipsnames option are supported. You may adjust change the option in above code to get other colors. For more details, see the package documentation of xcolor.

  3. The background color of the entire document can be set in LyX under "Document Settings > Colors". Unfortunately, it is not enough in this case. But it uses the LaTeX command \pagecolor, too.

pdf

e-birk
  • 4,373
  • Good approach! Can i use it also over a specific section (e.g. first page, last paragraph until second page, second paragraph) containing pictures and headings? – drofluga May 30 '13 at 18:57
  • @drofluga Unfortunately, this approach works for entire pages only. Pages cannot be colored partially in this way. – e-birk May 30 '13 at 22:54
2

The following MWE reproduces the problem:

\documentclass{article}
\usepackage{mdframed}% http://ctan.org/pkg/mdframed
\begin{document}
\begin{mdframed}
Here is some text.
\begin{figure}
  This is a figure.
\end{figure}
\end{mdframed}
\end{document}

Fundamentally you cannot include a float inside your mdframed environment (which yours is if using the definition from Create new paragraph style in LyX). You have two options for getting around this:

  1. Locate your float outside the mdframed environment and allow it to properly float; or

  2. Add the float package to your document preamble (\usepackage{float}) and use the [H] float specifier. This would allow you to set the "float" inside an mdframed environment.

Werner
  • 603,163