1

I try to rename environment codeexample used in tikz document, but fail to use the tricks as in Wrap an environment.

This is related to tkzexample: source and output side-by-side. The pgfmanual-en-macros.tex is used in \input{pgfmanual-en-macros.tex} (I do not know how to minimize it and upload as a attachment)

\documentclass{article}
\usepackage[usenames,dvipsnames]{xcolor}
\usepackage{tikz}
\usepackage{amsmath,amssymb}
\usepackage{tkzexample} 
\usepackage[a4paper,left=2.25cm,right=2.25cm,top=2.5cm,bottom=2.5cm,nohead]{geometry}%pgfmanual-en-main-preamble.tex

\input{pgfmanual-en-macros.tex}
\let\bcExample\codeexample
\let\ecExample\endcodeexample
\renewenvironment{codeexample}  {\bcExample[]}  {\ecExample}
\let\cExample\codeexample
\let\endcExample\endcodeexample
\newenvironment{eExample}   {\bcExample[]}  {\ecExample}

\begin{document}


When executed, that is, run via |pdflatex| or via |latex| followed by |dvips|,
the resulting will contain something that looks like this:
%
\begin{codeexample}
We are working on
\begin{tikzpicture}
  \draw (-1.5,0) -- (1.5,0);
  \draw (0,-1.5) -- (0,1.5);
\end{tikzpicture}.
\end{codeexample}

cExample

%\begin{cExample}
We are working on
\begin{tikzpicture}
  \draw (-1.5,0) -- (1.5,0);
  \draw (0,-1.5) -- (0,1.5);
\end{tikzpicture}.
%\end{cExample}

eExample

%\begin{eExample}
We are working on
\begin{tikzpicture}
  \draw (-1.5,0) -- (1.5,0);
  \draw (0,-1.5) -- (0,1.5);
\end{tikzpicture}.
%\end{eExample}

\end{document}

using renewenvironment{codeexample} works, but using \let\cExample\codeexample and \newenvironment{eExample} to rename/copy the new environment codeexample throw errors.

5
  • This code is compiled for me with TexLive 2019, correctly. Do you get compile error?
    – A Diyanat
    Commented Jul 25, 2019 at 2:59
  • 1
    @ChenDeng-Ta Can't you reduce this without including the huge pgfmanual-en-macros.tex which doesn't even appear to be the official one? With \usepackage[listings]{tcolorbox} \newtcblisting{codeexample}[1][]{#1} instead of the \input{pgfmanual-en-macros.tex}, I see no error either after uncommenting the \begin{...} and \end{...} markers for cExample and eExample.
    – frougon
    Commented Jul 25, 2019 at 5:46
  • @A-Diyanat and @frougon, I compiled it with XeLaTeX of TeXlive 2019. Error if you uncomment the cExample and eExample in the body. I am using codeexample extract from pgfmanual, it has syntax highlights. The one by \newtcblisting{codeexample} does not have syntax highlights. pgfmanual-en-macros.tex is beyond my scope, after delete 2 definitions, the \renewenvironment{codeexample} works as expected, I am not able to trim the redundant code. Commented Jul 25, 2019 at 8:07
  • 1
    @ChenDeng-Ta It doesn't matter if the presentation obtained with \newtcblisting{codeexample}[1][]{#1} doesn't match your final requirements. What matters to me is to be able to reproduce your error with a minimal example. I've just tried this with XeLaTeX under TL 2019 (with \usepackage[listings]{tcolorbox} \newtcblisting{codeexample}[1][]{#1} instead of the \input{pgfmanual-en-macros.tex}), this doesn't produce an error and seems to work fine. I hope someone else can reproduce the problem (by the way, double notifications with @ don't work here, unfortunately).
    – frougon
    Commented Jul 25, 2019 at 8:14
  • 1
    @frougon, the newtcblisting{...} way is answer in egreg's answer, where the tricks work. Here I am puzzled why the codeexample in pgfmanual throws error if using \codeexample and endcodeexample in cExample and eExample Commented Jul 25, 2019 at 8:28

1 Answer 1

1

I read the code in pgfmanual-en-macros.tex. I found that the environment endcodeexample is hard coded, which is defined by

  1. \def\codeexample[#1]
  2. \def\endcodeexample{\endgroup}
  3. \def\endofcodeexample#1
  4. in line 1502: \end{codeexample}[|endofcodeexample[#1]]|

Furthermore, the codeexample is hard coded in pgfmanual.sty with pgfmanual.prettyprinter.code.tex and pgfmanual.pdflinks.code.tex

1
  • Most importantly, \codeexample looks ahead for \endcodeexample, so renaming the beginning might work, but renaming the end will fail. Commented Sep 3, 2019 at 7:04

You must log in to answer this question.

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