5

The real problem here is that I can't figure out how to modify an existing environment. The concrete thing I want to achieve follows below.

I'm pretty happy with what the framed package gives me with its shaded* environment. But I'd like to change two things:

  1. The first paragraph in the environment should not start with indentation.

  2. I want to rename the environment from shaded* to something else.

    \documentclass{article}
    \usepackage{lipsum, xcolor, framed}
        \definecolor{shadecolor}{gray}{.8}
    
    \begin{document}
        \begin{shaded*}
            \lipsum[1-2]
        \end{shaded*}
    \end{document}
    

enter image description here

Sverre
  • 20,729

3 Answers3

4

Use the trick of \@afterheading (simplified):

\documentclass{article}
\usepackage{lipsum, xcolor, framed}
\definecolor{shadecolor}{gray}{.8}

\newenvironment{blurb}
 {\begin{shaded*}\everypar={{\setbox0=\lastbox}\everypar{}}}
 {\end{shaded*}}

\begin{document}

\begin{blurb}
\lipsum[1-2]
\end{blurb}

\end{document}

enter image description here

egreg
  • 1,121,712
  • You mean that this code is taken from what LaTeX does after a heading? – Sverre Apr 04 '15 at 14:51
  • 1
    @Sverre Yes, it's a nice trick that's also explained in the TeXbook. Since \everypar is invoked after the paragraph has been started and the indentation box has been inserted, {\setbox0=\lastbox} removes it (and \box0 doesn't even change, because it's done in a group). Then \everypar is reinitialized to empty. – egreg Apr 04 '15 at 14:57
4

Using tcolorbox for a change (for academic purposes).

\documentclass{article}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\definecolor{shadecolor}{gray}{.8}

\newtcolorbox{fancypar}[1][]{
    colback=shadecolor,
    boxsep=6pt,
    parbox=false,
    arc=0pt,
    outer arc=0pt,
    nobeforeafter,
    frame hidden,
    enhanced jigsaw,
    breakable,
    before=\par\noindent%
  }

\begin{document}

\begin{fancypar}
\lipsum[1-2]
\end{fancypar}

\end{document}

enter image description here

2

The following might be what you're after:

enter image description here

\documentclass{article}
\usepackage{lipsum, xcolor, framed}
\definecolor{shadecolor}{gray}{.8}

\makeatletter
\newenvironment{myfancyshade}
  {\@nameuse{shaded*}\noindent\ignorespaces}
  {\@nameuse{endshaded*}}
\makeatother

\begin{document}

\begin{myfancyshade}
  \lipsum[1-2]
\end{myfancyshade}
\end{document}

The old shaded* environment is invoked using \@nameuse, but using the traditional environment name works as well. Paragraph indentation is avoided via \noindent, and any spurious spaces is dropped using \ignorespaces.

Werner
  • 603,163