4

I want define a command like \maplebox; for example the command \maplebox{\latex}.

Why does the following code not compile?

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{lipsum}
 \usepackage[top=2.5cm,right=3.5cm,bottom=2.5cm,left=2.5cm]{geometry}      

\tcbuselibrary{listings,skins,theorems}

\newtcbinputlisting{\maplebox}[1][white]{
                                                            listing only,
                                                            nobeforeafter,
                                                            tcbox raise base,
%                                                           fontupper=\latinfont\setLTR\ttfamily\bfseries,
                                                            fontupper=\ttfamily\bfseries,
                                                            arc=0pt,
                                                            outer arc=0pt,
                                                            colback=#1!10!white,
                                                            colframe=#1!50!black,
                                                            boxsep=0pt,
                                                            left=1pt,
                                                            right=1pt,
                                                            top=2pt,
                                                            bottom=2pt,
                                                            boxrule=0pt,
                                                            bottomrule=1pt,
                                                            toprule=1pt}


\begin{document}

The \maplebox{\latex}

\end{document}

1 Answer 1

8

There are several problems / caveats. Firstly, \newtcbinputlisting creates a new listing input command, but you do not want to read a file, if I'm right. So, \newtcblisting should be used. This creates a verbatim environment, not a macro. It is not recommended to use such an environment with an optional parameter only. If you have a mandatory parameter, then you could use an optional parameter also.

Now, I transformed your example a little bit and I hope, it is useful for you:

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{lipsum}
\usepackage[top=2.5cm,right=3.5cm,bottom=2.5cm,left=2.5cm]{geometry}

\tcbuselibrary{listings,skins,theorems}

\newtcblisting{maplebox}[1]{
  listing only,
  colupper=black,
  nobeforeafter,
  hbox,
  tcbox raise base,
  fontupper=\ttfamily\bfseries,
  arc=0pt,
  outer arc=0pt,
  colback=#1!10!white,
  colframe=#1!50!black,
  boxsep=0pt,
  left=1pt,
  right=1pt,
  top=2pt,
  bottom=2pt,
  boxrule=0pt,
  bottomrule=1pt,
  toprule=1pt,
  }

\begin{document}

The
\begin{maplebox}{white}
\latex
\end{maplebox}
\
\begin{maplebox}{red}
\latex
\end{maplebox}
\
\begin{maplebox}{blue}
\latex
\end{maplebox}
\
\begin{maplebox}{green}
\latex
\end{maplebox}
\ test.

\end{document}

enter image description here

Update: With help of the xparse package, a macro solution is possible. It uses \lstinline from package listings embedded in a \tcbox from package tcolorbox. Formatting options are set directly to \lstinline.

\documentclass{article}

\usepackage{xparse}
\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{lipsum}
\usepackage[top=2.5cm,right=3.5cm,bottom=2.5cm,left=2.5cm]{geometry}

\tcbuselibrary{listings,skins,theorems}

\NewDocumentCommand{\maplebox}{ O{white} v}{%
  \tcbox[colupper=black,nobeforeafter,tcbox raise base,
    arc=0pt,outer arc=0pt,colback=#1!10!white,
    colframe=#1!50!black,
    boxsep=0pt,left=1pt,right=1pt,top=2pt,bottom=2pt,
    boxrule=0pt,bottomrule=1pt,toprule=1pt]{%
  \lstinline[style=tcblatex,texcsstyle=*\color{blue}\bfseries]§#2§}}

\begin{document}

\maplebox{\LaTeX}
\maplebox[red]{Some \LaTeX\ code}
\maplebox[blue]{$a^2+b^2=c^2$}
\maplebox[green]{\textbf{something}}

\end{document}

enter image description here

Final Update: The same output can be produced with a more elegant syntax using tcolorbox from version 2.80 (2014/03/31):

\documentclass{article}

\usepackage[svgnames]{xcolor}
\usepackage{tcolorbox}
\usepackage{amsmath,amsfonts,amssymb,amsthm}
\usepackage{lipsum}
\usepackage[top=2.5cm,right=3.5cm,bottom=2.5cm,left=2.5cm]{geometry}

\tcbuselibrary{listings,skins,theorems,xparse}

\NewTotalTCBox{\maplebox}{ O{white} v}
{ colupper=black,nobeforeafter,tcbox raise base,
  arc=0pt,outer arc=0pt,colback=#1!10!white,
  colframe=#1!50!black,
  boxsep=0pt,left=1pt,right=1pt,top=2pt,bottom=2pt,
  boxrule=0pt,bottomrule=1pt,toprule=1pt}
{\lstinline[style=tcblatex,texcsstyle=*\color{blue}\bfseries]§#2§}

\begin{document}

\maplebox{\LaTeX}
\maplebox[red]{Some \LaTeX\ code}
\maplebox[blue]{$a^2+b^2=c^2$}
\maplebox[green]{\textbf{something}}

\end{document}
3
  • Thanks @Sturm, but i can define an environment for my purpose, but it seems not good and easy using an environment for one word in a line, how can define a command? thanks in advance :)
    – niksirat
    Mar 19, 2014 at 22:05
  • 1
    @user626 Well, you can't. At least, not with tcolorbox and verbatim content. You could use \lstinline from the listings package instead (without boxing). But, if you really have just one word, you could also use a normal tcolorbox or \tcbox and mask the critical characters like \tcbox{\textbackslash latex}. Mar 20, 2014 at 9:32
  • 1
    @user626 Now, I have another solution, if the package xparse is loaded additionally. Everything put together gives the \maplebox command. I updated my answer with the new code. Mar 26, 2014 at 15:30

You must log in to answer this question.

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