5

I know it must be something trivial:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}

\usepackage[svgnames]{xcolor}
\usepackage{framed} %http://www.latex-community.org/forum/viewtopic.php?f=44&t=5823
\colorlet{shadecolor}{gray!25}
\renewenvironment{leftbar}{%
  \def\FrameCommand{\textcolor{shadecolor}{\vrule width 3pt} \hspace{10pt}}%
  \MakeFramed {\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}

\declaretheorem[name=Example]{exampleOri}
\newenvironment{example}[1]{\begin{leftbar}\begin{exampleOri}[#1]}{\end{exampleOri}\end{leftbar}}

\begin{document}

\begin{example}[This is a test]
Bla
\end{example}

\end{document}

Which results in Example 1 ([). This is a test] Bla. What is wrong here?

UPDATE: As it's not directly an answer to the question, but still useful, I wanted to post my final approach using the mdframed key in thmtools:

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}
\usepackage[framemethod=TikZ]{mdframed}

\usepackage[svgnames]{xcolor}
\colorlet{shadecolor}{gray!25}

\declaretheorem[name=Example,
    mdframed={
        hidealllines=true,leftline=true,
        rightmargin=0pt,innerrightmargin=0pt,
        innerlinewidth=0pt,middlelinewidth=0pt,
        outerlinecolor=shadecolor,outerlinewidth=3pt,
        skipabove=\topskip,skipbelow=\topskip}
    ]{example}

\begin{document}
\begin{example}[This is a test]
   Bla
\end{example}    
\end{document}
4
  • 1
    The argument to example is declared as mandatory, but you're using it with the syntax for optional arguments.
    – egreg
    Mar 1, 2013 at 15:10
  • Ah! But if I use \newenvironment{example}[1][] then I get an error when I don't supply the argument. How does this work with \begin{exampleOri}[#1] then?
    – letmaik
    Mar 1, 2013 at 15:15
  • I just figured a way out: \newenvironment{example}[1][ ], a white space. Is this the right way?
    – letmaik
    Mar 1, 2013 at 15:16
  • No, it's wrong: you'll get parentheses anyway.
    – egreg
    Mar 1, 2013 at 15:21

1 Answer 1

8

You're declaring the argument to example as mandatory, so it should be enclosed in braces.

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{thmtools}

\usepackage[svgnames]{xcolor}
\colorlet{shadecolor}{gray!25}

\usepackage{framed}
\renewenvironment{leftbar}{%
  \def\FrameCommand{\textcolor{shadecolor}{\vrule width 3pt} \hspace{10pt}}%
  \MakeFramed {\advance\hsize-\width \FrameRestore}}%
{\endMakeFramed}

\declaretheorem[name=Example]{exampleOri}
\newenvironment{example}[1][]
  {\begin{leftbar}
   \ifx\relax#1\relax
     \begin{exampleOri}
   \else
     \begin{exampleOri}[#1]
   \fi}
  {\end{exampleOri}\end{leftbar}}

\begin{document}

\begin{example}[This is a test]
Bla
\end{example}

\begin{example}
Bla
\end{example}

\end{document}

enter image description here

However, I'd take an easier route.

\documentclass{article}

\usepackage{amsmath}
\usepackage{amsthm}

\usepackage[svgnames]{xcolor}
\colorlet{shadecolor}{gray!25}

\usepackage[framemethod=TikZ]{mdframed}
\newmdtheoremenv[hidealllines=true,leftline=true,
  innerlinewidth=0pt,middlelinewidth=0pt,
  outerlinecolor=shadecolor,outerlinewidth=3pt,
  skipabove=\topsep,skipbelow=\topsep,
  ]{example}{Example}

\begin{document}

\begin{example}[This is a test]
Bla
\end{example}

\begin{example}
Bla
\end{example}

\end{document}

enter image description here

2
  • Thanks for the answer! :) I just wanted to mention that in my case using \leftbar is easier because then I can reuse my existing thmtools styles which I would have to recreate in mdframed.
    – letmaik
    Mar 1, 2013 at 15:57
  • I discovered the mdframed key in thmtools. See the update I posted. Now I have the best of both worlds :)
    – letmaik
    Mar 2, 2013 at 14:10

You must log in to answer this question.

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