1

I have defined a custom environment with an option to either display a title or not (using the \xifthen package, I am not sure if that is the best approach but works so far).

\documentclass{report}
\usepackage{xifthen}

\newcounter{mycnt}[chapter]%
\renewcommand{\themycnt}{\thechapter.\arabic{mycnt}}
\newenvironment{myenv}[1][]{%
  \ifthenelse{\equal{#1}{notitle}}
{\par\medskip\noindent}
{
    \refstepcounter{mycnt}%
    \par\medskip\noindent\textbf{Test\ \themycnt}%
    \par\medskip\noindent}
}{%
  \par\medskip\ignorespacesafterend\noindent%
}

\begin{document}
\chapter{Introduction} 
\section{A Section} 
Some text
\begin{myenv} \label{test:one} 
Some text within environment (why does it begin with that weird space?)
\end{myenv}
As Test~\ref{test:one} demonstrates ...

\begin{myenv}[notitle]  
Some text within environment with no title
\end{myenv}

\begin{myenv} \label{test:two} 
Some text within environment 
\end{myenv}
As Test~\ref{test:two} demonstrates ...

\section{Another Section} 
Now let us look again at Test~\ref{test:one} already shown in the previous section:

\begin{myenv}  % maybe another option, such as [repeated] ?
Some text within environment (how to get this to be repeated as Test 1.1?)
\end{myenv}
\end{document}

Now, aside from the option to not show the title (and thus also not step the counter), in my document I would also like to be able to have a second option to repeat the contents of an environment already printed earlier, so that I can refer to Test 1.1, for example, in a subsequent section or chapter again, but still reflecting the number from where it first occurred, i.e., 1.1.

enter image description here

BTW, there seems to be also a weird space in the first line of the environment, where I have no clue where it's coming from. Anyone able to spot the error?

Note: In the end I plan to include the individual parts using the environment by using \input{filename.tex}. Ideally I'd like to have the \begin{myenv} label{somelabel} line in that filename.tex, so if there was a way to let it repeat the numbering automatically when it finds a label that is repeated that'd be wonderful. Or is that too much to ask for? If this is too complicated I could also \begin my environment before \inputting the file.


EDIT: Maybe for the latter scenario it would in fact better not to \begin, \label, and \end the environment within the file to \input but rather to define a command that would take as argument the {filename} and execute

\begin{myenv}\label{#1}\input{#1}\end{myenv}

Nonetheless I would first need help figuring out how to implement the option that would let me repeat environments. Any suggestions highly appreciated.


EDIT2: Clarification: Ideally I'd want to have three options of repeating an environment.

(1) The contents without a title (this works fine as I've implemented it but maybe there's a better way):

\begin{myenv}[notitle]\input{file1}\end{myenv}

(2) Repeat the same contents and also repeat the same title.

\begin{myenv}[repeatedtitle]\input{file1}\end{myenv}

(3) Repeat only part of the content (i.e., \input a different file) but repeat the title.

\begin{myenv}[repeatedtitle]\input{partoffile1}\end{myenv}

6
  • What do you mean by "repeat"-ing some myenv? Do you want the entire contents to be repeated? Or do you want just the \label to reference something previously mentioned.
    – Werner
    Oct 17, 2016 at 3:39
  • @Werner I'd want the contents to be repeated. So a reader doesn't have to flip pages and has the content again right there. But without it being given a new number.
    – jan
    Oct 17, 2016 at 6:12
  • Would you repeat the contents manually (I don't know whether your setup generates the contents automatically or not), or do you want to have a way of saying "repeat the entire contents of X"?
    – Werner
    Oct 17, 2016 at 6:13
  • @Werner If there is a general mechanism to have both options that'd be ideal. The main scenario I was thinking about I'd want to simply \input the same external file a second time, but now I'm thinking it could also be useful to be able to repeat the label number and in the content only put, let's say, lines 1-2 of a "test" that has a total of 5 lines.
    – jan
    Oct 17, 2016 at 6:20
  • 1
    I honestly think more examples showing your input and the expected output would be valuable. I'd suggest using a key-value interface. That is, \begin{myenv}[label=X] ... \end{myenv}, \begin{myenv}[title=false] ... \end{myenv}, \begin{myenv}[repeat=X] ... \end{myenv} ...
    – Werner
    Oct 17, 2016 at 6:24

1 Answer 1

1

I propose a slightly different environment interface using a key-value approach. The keys that are defined include

  • title: A boolean key that can be true (default) or false to set the title Test X.Y or not.

  • notitle: Similar to title=false

  • label: Equivalent to setting a \label inside the environment

  • repeat: The name of an existing label that is used in a repeated title.

enter image description here

\documentclass{report}

\usepackage{xkeyval}

\makeatletter
\define@boolkey{fam}[myenv@]{title}[true]{}
\define@boolkey{fam}[myenv@]{notitle}[true]{\ifmyenv@notitle\myenv@titlefalse\else\myenv@titletrue\fi}
\define@cmdkey{fam}[myenv@]{label}[\relax]{}
\define@cmdkey{fam}[myenv@]{repeat}[\relax]{}

\newcounter{mycnt}[chapter]%
\renewcommand{\themycnt}{\thechapter.\arabic{mycnt}}
\newenvironment{myenv}[1][]{%
  \setkeys{fam}{title,label,repeat,#1}%
  \ifmyenv@title
    \par\addvspace{\medskipamount}%
    \expandafter\ifx\myenv@repeat\relax
      \refstepcounter{mycnt}%
      \expandafter\ifx\myenv@label\relax\else\label{\myenv@label}\fi
    \else
      \renewcommand{\themycnt}{\ref{\myenv@repeat}}%
    \fi
    \noindent\textbf{Test\ \themycnt}%
    \par\nobreak\addvspace{\medskipamount}%
  \else
    \par\addvspace{\medskipamount}%
  \fi
  \edef\@currentlabel{\themycnt}%
  \noindent\ignorespaces
}{%
  \par\addvspace{\medskipamount}\ignorespacesafterend
}
\makeatother

\begin{document}

\chapter{Introduction} 
\section{A Section} 
Some text
\begin{myenv}[label={test:one}]
A: Some text within environment
\end{myenv}
As Test~\ref{test:one} demonstrates ...

\begin{myenv}[notitle]
B: Some text within environment with no title
\end{myenv}

\begin{myenv}[label={test:two}]
C: Some text within environment 
\end{myenv}
As Test~\ref{test:two} demonstrates ...

\section{Another Section}
Now let us look again at Test~\ref{test:one} already shown in the previous section:

\begin{myenv}[repeat={test:one}]
D: Some text within environment
\end{myenv}

\end{document}

You need to use % to avoid the spurious spaces. This has been taken care of as part of the label key-value feature.

1
  • I wanted to come back to this answer, I was wondering is there maybe a way to easily generate a toc list of this environment? One that would then also print the repeated tests? I've tried to experiment with this code but wasn't successful yet :/
    – jan
    Dec 7, 2016 at 11:14

You must log in to answer this question.

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