7

I once defined a customized latex command, which works well. And now I would like to convert it into a latex environment using \newenvironment command. However, I failed after trying several ways. Could you help me, please?

The definition of my own latex command is

\newcommand{\smallbox}[1]{
    \tikzstyle{mybox} = [draw=red, rectangle,inner sep=5pt, inner ysep=5pt]
    \begin{center}
        \begin{tikzpicture}
            \node [mybox] (box){
                \begin{minipage}{0.85\textwidth}
                     #1
                \end{minipage}
            };
       \end{tikzpicture}
    \end{center}
}

Let's call the to-be-defined enviroment "smallenvironment" for the moment.

And I would like that, the following command

\smallbox{This is a small box!}

is equivalent to

\begin{smallenvironment}
    This is a small box!
\end{smallenvironment}

Thanks!

3
  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – jub0bs
    Apr 12, 2014 at 10:31
  • 1
    I'm not sure you can split a node like that. It seems you only want to draw a frame around the minipage... have you considered using the mdframed package instead?
    – jub0bs
    Apr 12, 2014 at 10:35
  • Why do you want to convert it into an environment? Is there some 'begin code' and 'end code'? Which content is to come before or after the box? And did you have a look on \tcolorbox package, e.g.?
    – user31729
    Apr 12, 2014 at 10:37

2 Answers 2

7

With the help of \NewEnviron from the environ package

\documentclass{article}

\usepackage{tikz}
\usepackage{environ}

\NewEnviron{smallenvironment}{%
    \tikzstyle{mybox} = [draw=red, rectangle,inner sep=5pt, inner ysep=5pt]%
    \begin{center}%
        \begin{tikzpicture}%
            \node [mybox] (box){%
                \begin{minipage}{0.85\textwidth}%
                     \BODY
                \end{minipage}%
            };%
       \end{tikzpicture}%
    \end{center}%
}

\begin{document}

\begin{smallenvironment}
This is a small box!
\end{smallenvironment}

\end{document} 

enter image description here

Another way, as egreg notes in his comment, is to keep your definition of \smallbox and simply say

\NewEnviron{smallenvironment}{\smallbox{\BODY}}

EDIT

Note that an environment like the above can be obtained in a much simpler way with the help of the mdframed package.

Take a look at the following MWE

\documentclass{article}

\usepackage{tikz}
\usepackage{environ}
\usepackage{mdframed}

\newcommand{\smallbox}[1]{
    \tikzstyle{mybox} = [draw=red, rectangle,inner sep=5pt, inner ysep=5pt]
    \begin{center}
        \begin{tikzpicture}
            \node [mybox] (box){
                \begin{minipage}{0.85\textwidth}
                     #1
                \end{minipage}
            };
       \end{tikzpicture}
    \end{center}
}

\NewEnviron{smallenvironment}{\smallbox{\BODY}}

\newmdenv[%
    linecolor=red,
    innerleftmargin=5pt,
    innerrightmargin=5pt,
    leftmargin=20pt,
    rightmargin=20pt
  ]{smallmdframe}

\begin{document}

\begin{smallenvironment}
This is a small box!
\end{smallenvironment}

\begin{smallmdframe}
This is a small box!
\end{smallmdframe}

\end{document} 

and at its output

enter image description here

4
  • Why not \NewEnviron{smallenv}{\smallbox{\BODY}} keeping the command definition?
    – egreg
    Apr 12, 2014 at 10:52
  • @egreg You're right. I'll add it in the answer. Apr 12, 2014 at 10:59
  • 1
    Having both forms can be handy. However, if the purpose is just to draw a frame, using mdframed or tcolorbox could be better.
    – egreg
    Apr 12, 2014 at 11:00
  • @user49768 Remember that you can accept the answer that helped you most. See How do you accept an answer? Apr 12, 2014 at 15:44
3

Karl has already shown you about forming a newenvironment using environ. I will point out some improvements to your tikzpicture code.

  1. Use tikzset instead of tikzstyle.
  2. No need of specifying ysep when you have already inner sep.
  3. No need of using minipage inside the node. You can specify text width= 0.85\textwidth, minimum width = 0.85\textwidth in node options, making it more tikz way.
  4. Instead of begin{center} and \end{center} you may use \centering if you want to avoid extra vertical space.

Code:

\documentclass{article}

\usepackage{tikz}
\usepackage{environ}

\NewEnviron{smallenvironment}{%
    \tikzset{mybox/.style = {draw=red, rectangle,inner sep=5pt}}%
    \begin{center}%     %% \centering         
        \begin{tikzpicture}%
            \node [mybox,text width= 0.85\textwidth, minimum width = 0.85\textwidth] (box){%
               \BODY
            };%
       \end{tikzpicture}%
       \end{center}    %% \par %% if you use \centering
}

\begin{document}
Some text

\begin{smallenvironment}
This is a small box!
\end{smallenvironment}

Some text
\end{document}

enter image description here

With \centering, you get

enter image description here

You must log in to answer this question.

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