5

This code works well [edit : thanks, Mico]

\documentclass{minimal}
\usepackage{tikz}
\usepackage{lipsum}

\newenvironment{MyBox}[1]{%
  \begin{tikzpicture}  
  \node[text width = 0.9\linewidth, text justified] (box) {#1};
  }%
  {\end{tikzpicture}\par \smallskip}

\begin{document}

\begin{MyBox}{%
 \lipsum[1]
}\end{MyBox}%

\end{document}

I would like to use a command like this :

    \begin{MyBox}
    \lipsum[1]
    \end{MyBox}

rather than :

\begin{MyBox}{%
 \lipsum[1]
}\end{MyBox}%

Is there a simple way to do that ?

Thanks

1
  • Your code doesn't compile because of \documentclass[minimal]. Please fix.
    – Mico
    Commented Aug 21, 2022 at 20:27

2 Answers 2

2

The problem is that #1 is the first nonspace token or braced group after \begin{MyBox}. Without the braces the argument turns out to be \lipsum, so you get seven paragraphs and [1] gets ignored and you can see

Missing character: There is no [ in font nullfont!
Missing character: There is no 1 in font nullfont!
Missing character: There is no ] in font nullfont!

in your log file.

It would be even worse if you have real text in the environment: for instance,

\begin{MyBox}
Lorem ipsum
\end{MyBox}

would just typeset “L” and ignore the rest.

You have two choices. The simplest one is to define a macro:

\newcommand{\MyBox}[1]{%
  \par\addvspace{\smallskipamount}%
  \begin{tikzpicture}
  \node[text width = 0.9\linewidth, text justified] (box) {\ignorespaces #1\par};
  \end{tikzpicture}%
  \par\addvspace{\smallskipamount}%
}

and use it as

\MyBox{
  Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
  Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae,
  felis. Curabitur dictum gravida mauris. Nam arcu libero,
  nonummy eget, consectetuer id, vulputate a, magna.
}

or absorb the contents of the environment in order to pass it to \node. This is easy with the +b argument specifier to \NewDocumentEnvironment. No package is needed with a recent (not earlier than October 2020) LaTeX. For earlier releases, add \usepackage{xparse}.

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}

\NewDocumentEnvironment{MyBox}{O{}+b}
 {%
  \par\addvspace{\smallskipamount}%
  \begin{tikzpicture}[#1]
  \node[text width = 0.9\linewidth, text justified] (box) {#2};
  \end{tikzpicture}
  \par\addvspace{\smallskipamount}%
 }{}

\begin{document}

\begin{MyBox}
\lipsum[1]
\end{MyBox}

\end{document}

Here I added an optional argument so you can pass global options to the tikzpicture, say

\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}

\NewDocumentEnvironment{MyBox}{O{}+b}
 {%
  \par\addvspace{\smallskipamount}%
  \begin{tikzpicture}[#1]
  \node[text width = 0.9\linewidth, text justified] (box) {#2};
  \end{tikzpicture}
  \par\addvspace{\smallskipamount}%
 }{}

\begin{document}

\begin{MyBox}[every node/.style={color=blue}]
\lipsum[1]
\end{MyBox}

\end{document}
3

This solution uses the environ package which allows capturing the contents of the environment into the \BODY macro, which then can be used elsewhere.

a

    \documentclass[12pt,a4paper]{article}

\usepackage{tikz}
\usepackage{lipsum}

\newenvironment{MyBox}[1]{% Old definition
    \begin{tikzpicture}  
        \node[text width = 0.9\linewidth, text justified] (box) {#1};
    }%
    {\end{tikzpicture}\par \smallskip}


%%**************************************** added <<<<<<<<<<<<<<,
\usepackage{environ} % needed <<<

\NewEnviron{MyBoxALT}[1][]{% new definition <<<<<<<<<<<<<<<<<<<
    \begin{tikzpicture}[#1]  
        \node[text width = 0.9\linewidth, text justified] (box) {\BODY};
    \end{tikzpicture}\par \smallskip%   
}
%%****************************************

\begin{document}
    
    Environment \verb|MyBox|
    
    \begin{MyBox}{%
            1. \lipsum[1]
    }\end{MyBox}%
    
    \bigskip
    
    Environment \verb|MyBoxALT|
    
    \begin{MyBoxALT} % added <<<<<<<<<<<<<<<<<
        2. \lipsum[1]
    \end{MyBoxALT}%

    \begin{MyBoxALT}[every node/.style={color=blue}] % added <<<<<<<<<<<<<<<<<
    3. \lipsum[1]
     \end{MyBoxALT}%
    
    
\end{document}

You must log in to answer this question.

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