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}
\documentclass[minimal]
. Please fix.