Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to define a new environment like

\begin{mybox}
  some text
\end{mybox}

that would automatically expand to something like

\begin{tikzpicture}
  \node[fill=red!0.5] {some text};
\end{tikzpicture}

I tried to define an environment with the following code in the preamble:

\newenvironment{mybox}{\begin{tikzpicture}\node[fill=red!0.5]\bgroup}{\egroup;\end{tikzpicture}}

but the color option is not fully working. If fill=red is used it works correctly, but if a mixed color like fill=red!0.5 it doesn't. What can be done to mix colors?

EDIT: The questions contains an error. It should be red!50 instead of red!0.5. Anyway, the answers below can be helpful as alternatives to generate a colorful text box.

share|improve this question
4  
\bgroup is not the same as { and can not be used to delimit a macro argument Th eargumnet to \node in your version is not {some text}` but just \bgroup. –  David Carlisle Jan 22 at 15:46
1  
Welcome to TeX.SX! –  karlkoeller Jan 22 at 16:24
2  
Blend values are between 0 and 100. In this context, 0.5 is very close to zero. So red!0.5 is very close to white. ;-) –  Paul Gaborit Jan 22 at 16:39
    
@Paul Gaborit: You are right, silly me! I should have used red!50 instead of red!0.5. –  Miguel Jan 23 at 9:28
    
@David Carlisle: I followed http://en.wikibooks.org/wiki/LaTeX/Macros#Unmatched_braces which says that \bgroup and { are synonyms and recommends its use precisely in this situation. –  Miguel Jan 23 at 9:38
show 1 more comment

2 Answers

up vote 9 down vote accepted

It seems to me that fill=red!0.5 isn't distinguishable from white...

Anyway, for these kind of environment definitions, \NewEnviron from the environ package can be useful.

\NewEnviron{mybox}{%
  \begin{tikzpicture}%
    \node[fill=red!10]{\BODY};%
  \end{tikzpicture}%
}

Note that I've changed red!0.5 to red!10 to make it distinguishable.

In the following MWE I've also left your definition and it seems to work.

\documentclass{article}
\usepackage{environ}
\usepackage{tikz}

\NewEnviron{mybox}{%
  \begin{tikzpicture}%
    \node[fill=red!10]{\BODY};%
  \end{tikzpicture}%
}

\newenvironment{oldmybox}{%
  \begin{tikzpicture}\node[fill=red!10]\bgroup%
}{%
  \egroup;\end{tikzpicture}%
}

\begin{document}

\begin{mybox}Hello\end{mybox}

\begin{oldmybox}Hello\end{oldmybox}

\begin{tikzpicture}
  \node[fill=red!10]{Hello};
\end{tikzpicture}

\end{document} 

Output:

enter image description here

share|improve this answer
add comment

If you want your environment be expanded in a tikzpicture, I'll delete my answer but if you want an environment to produce some colorful box, \tcbox command from tcolorbox can help you. Next code shows how to define \myboxcommand or environment myboxenv which outputs will be like a \node[fill] command.

\documentclass{article}
\usepackage[skins]{tcolorbox}

\newtcbox{myboxcommand}{nobeforeafter, tcbox raise base,
arc=0pt, outer arc=0pt, colback=red!10, boxsep=0pt,
boxrule=0pt, left=.3333em, right=.3333em, top=.3333em, bottom=.3333em}

\newtcolorbox{myboxenv}{nobeforeafter,capture=hbox,tcbox raise base,
arc=0pt, outer arc=0pt, colback=red!10, boxsep=0pt,
boxrule=0pt, left=.3333em, right=.3333em, top=.3333em, bottom=.3333em}

\begin{document}

\myboxcommand{Hello}

\begin{myboxenv}
Hello
\end{myboxenv}

\begin{tikzpicture}
  \node[anchor=base,fill=red!10]{Hello};
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.