0

Sorry if this has been asked before, but I couldn't find an answer, that fits my situation. I want to create a \newenvironment that produces a box, with a predefined color, and can have text inside. I already have an example of how it should look in LaTeX:

enter image description here

Which uses this code:

\begin{center}
    \definecolor{lightgray}{cmyk}{0,0,0,.04}
    \setlength{\fboxsep}{10pt}\colorbox{lightgray}{\parbox{0.95\textwidth}{\blindtext}}
\end{center}

I have tried doing this:

\newenvironment{boksi}
{
    \begin{center}
    \definecolor{lightgray}{cmyk}{0,0,0,.04}
    \setlength{\fboxsep}{10pt}\colorbox{lightgray}{\parbox{0.95\textwidth}}
}
{
    \end{center}
}

Which produces this:

enter image description here

Is there anyone, who can tell me what I am doing wrong and/or what I should do instead?

1
  • There are multiple solutions to this question; I, for one, have done exactly what you're trying to do with the definition a new mdframed environment, e.g. : \newmdenv[backgroundcolor=gray!20, hidealllines, linewidth=0pt]{boksi} Now this question indeed has probably been already asked somewhere...
    – v_manz
    Jan 27, 2022 at 22:54

2 Answers 2

1

Perhaps this does the trick:

\documentclass{article}
\usepackage{blindtext}
\usepackage{xcolor}
\definecolor{lightgray}{cmyk}{0,0,0,.04}

\newbox\scratchbox
\newcommand\PassFirstToSecond[2]{#2{#1}}

\makeatletter
\newenvironment{boksi}{%
  \expandafter\PassFirstToSecond\expandafter{\the\fboxsep}{%
    \setlength{\fboxsep}{10pt}%
    \begin{lrbox}{\scratchbox}%
    \begin{minipage}{\dimexpr\textwidth-2\fboxsep\relax}%-2\fboxrule\relax}%
    % \centering
    \setlength\fboxsep
  }%
}{%
    \end{minipage}%
    \end{lrbox}%
    \trivlist\item\relax\if@minipage\else\vskip\parskip\fi
    % \fcolorbox{black}{lightgray}{\usebox{\scratchbox}}%
    \colorbox{lightgray}{\usebox{\scratchbox}}%
    \if@newlist\leavevmode\fi\endtrivlist 
}%
\makeatother

\fboxsep=.5\fboxsep

\begin{document}

\noindent text \hfill text \hfill text \hfill text

\begin{boksi}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Etiam lobortis \fbox{facilisis} sem. \blindtext
\end{boksi}

\noindent text \hfill text \hfill text \hfill text

\end{document}

enter image description here

0

Thank you to v_manz and Ulrich Diez for their answers. I ultimately went with v_manz solution to the problem, but Ulrich Diez' works aswell.

Here's the final code, using the mdframed package:

\newmdenv[backgroundcolor=gray!4, hidealllines, linewidth=0pt, innertopmargin=10pt, innerbottommargin=10pt]{synopboks}

which produces this:

enter image description here

1
  • Note: I would probably use tcolorbox instead. It can do a LOT more than mdframed.
    – daleif
    Jan 28, 2022 at 9:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .