3

I want to read code from external file in tcolorbox in new defined \texexptitled environment but could not succeeded (See MWE). Without this my MWE also throws two other errors pdflatex> ! paragraph ended before \tcb@set@label was complete.. Any help will be highly appreciated. Thanks in advance.


MWE


\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%


\usepackage{filecontents}
\begin{filecontents*}{exampleTest.tex}
   Welcome to \LaTeX.
\end{filecontents*}


%Definition of New Environments
\tcbset{
texexp/.style={colframe=green!85!black, colback=red!5!white,
coltitle=red!50!yellow!3!black, left=6mm,
listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}},
fonttitle=\small\textsc\bfseries, fontupper=\small, fontlower=\small},
example/.style 2 args={texexp,
title={Listing \thetcbcounter: #1},label={#2}},
}


\newtcblisting{texexp}[1]{texexp,#1}

\newtcblisting[auto counter,number within=section]{texexptitled}[3][]{%
example={#2}{#3},#1}

\newtcolorbox[use counter from=texexptitled]{texexptitledspec}[3][]{%
example={#2}{#3},#1}


\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}


\tcbset{listing engine={listings}}
\begin{document}

% Redefine the document environment within a group

\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\tcbinputlisting{%
  listing file=exampleTest.tex
, colback=red!5!white
, colframe=red!25
, left=6mm
, listing options={style=tcblatex, numbers=left, numberstyle=\tiny\color{red!75!black}}
 }

\endgroup

\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\begin{texexptitled}{Example}

\begin{document}
Here, we use Example  with a title line.
\end{document}

\end{texexptitled}

\endgroup



\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\texexptitledinputlisting{% Just tried. Not Working.
  listing file=exampleTest.tex
, colback=red!5!white
, colframe=red!25
, left=6mm
, listing options={style=tcblatex, numbers=left, numberstyle=\tiny\color{red!75!black}}
 }

\endgroup


\end{document}
6
  • 1
    You forgot the 2nd argument to, which is designed to hold the label for \begin{texexptitled}{Example}{mylabel}, according to the manual
    – user31729
    Aug 18 '14 at 19:21
  • You @ChristianHupfer are savior for me. Thanks a lot.
    – MYaseen208
    Aug 18 '14 at 19:27
  • There is still one error left, I try to figure out...
    – user31729
    Aug 18 '14 at 19:28
  • \texexptitledinputlisting is not defined. You have to use the \newtcbinputlisting command for that.
    – user31729
    Aug 18 '14 at 19:32
  • Other error is caused by undefined \texexptitledinputlisting environment. Looking for a command to read code from external file for texexptitled environment.
    – MYaseen208
    Aug 18 '14 at 19:33
3

I'm not sure, if I understood your intention completely. But, I corrected the errors Christian Hupfer already mentioned, and I made a new macro \texexptitledinputlisting which hopefully helps for your own extensions. I kept the rest unchanged with small exceptions / corrections.

\documentclass{article}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{tcolorbox}
\tcbuselibrary{listings}%


\usepackage{filecontents}
\begin{filecontents*}{exampleTest.tex}
   Welcome to \LaTeX.
\end{filecontents*}


%Definition of New Environments
\tcbset{
  texexp/.style={colframe=green!85!black, colback=red!5!white,
  coltitle=red!50!yellow!3!black, left=6mm,
  listing options={style=tcblatex,numbers=left,numberstyle=\tiny\color{red!75!black}},
  fonttitle=\small\scshape\bfseries, fontupper=\small, fontlower=\small},
  example/.style 2 args={texexp,title={Listing \thetcbcounter: #1},label={#2}},
}


\newtcblisting{texexp}[1]{texexp,#1}

\newtcblisting[auto counter,number within=section]{texexptitled}[3][]{%
  example={#2}{#3},#1}

\newtcolorbox[use counter from=texexptitled]{texexptitledspec}[3][]{%
  example={#2}{#3},#1}

\newtcbinputlisting[use counter from=texexptitled]{\texexptitledinputlisting}[4][]{%
  example={#3}{#4},listing file=#2,#1}

\lstset{ % General setup for the package
    language={[LaTeX]TeX},
    basicstyle=\small\sffamily,
    numbers=left,
    numberstyle=\tiny,
    frame=tb,
    tabsize=4,
    columns=fixed,
    showstringspaces=false,
    showtabs=false,
    keepspaces,
    commentstyle=\color{red},
    keywordstyle=\color{blue}
}


\tcbset{listing engine={listings}}
\begin{document}

\section{Test}

% Redefine the document environment within a group

\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\tcbinputlisting{%
  listing file=exampleTest.tex, colback=red!5!white,
  colframe=red!25, left=6mm,
  listing options={style=tcblatex, numbers=left, numberstyle=\tiny\color{red!75!black}}
}

\endgroup

\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\begin{texexptitled}{Example}{examplelabel}
\begin{document}
Here, we use Example  with a title line.
\end{document}
\end{texexptitled}

\endgroup


\begingroup
\renewenvironment{document}{}{}
\renewcommand\documentclass[2][]{}

\texexptitledinputlisting[
  colback=red!5!white,
  colframe=red!25,
  left=6mm,
  listing options={style=tcblatex, numbers=left, numberstyle=\tiny\color{red!75!black}}]%
  {exampleTest.tex}{Example title}{examplelabel2}

\endgroup

\end{document}

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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