Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have sought long and hard for a solution to this and experimented with a combination of http://tex.stackexchange.com/a/43351 and http://tex.stackexchange.com/a/443, but could not get it to work. I would like the following to happen:

  • I define an example somewhere in the text like this

    \begin{shortex}\label{Example1} lorem ipsum \end{shortex}
    
  • Later in the document, I introduce an altered version of the example (Example 1'), if possible using cleveref

Here's my code before any of that happens:

\documentclass[letterpaper,12pt]{article} 

%Math symbol packages
  \usepackage{amsmath}
  \usepackage{amsfonts}
  \usepackage{amssymb}

%Add several formatting packages
  \usepackage{paralist}
  \usepackage[colorlinks=true,hidelinks]{hyperref}
  \usepackage{cleveref}
  \crefdefaultlabelformat{[#2#1#3]}

%Add theorem support
  \usepackage{amsthm}

%Define example
  \theoremstyle{definition}
  \newtheorem{ex}{Example}
  \renewcommand{\theex}{\arabic{ex}}

%Access package-internal code that uses @

\makeatletter

  % Define examples without linebreaks
  \newenvironment{shortex}
  {\refstepcounter{ex}\textsc{Example~}\theex:\normalfont}%\begin{shortex} 
    {}%\end{shortex}

\makeatother

%Define cref for Examples
  \crefname{ex}{}{examples}
  \creflabelformat{ex}{(#2#1#3)}

\begin{document}
   \begin{shortex}\label{ex1} This is example 1 \end{shortex}
\end{document}

I have tried getting rid of shortex and simply using {ex}, but this still wouldn't work.

Thanks for any help y'all can give!

EDIT (djupp): I also found David Carlisle's http://tex.stackexchange.com/a/69918/27148 now, which would probably work great if I could incorporate the suggested use of \ref (or \cref) to refer back to an older example. (I.e., it's possible that I would want to restate example 1 after I've already stated 2,3,4,5,etc.)

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

I don't know if this is what you're looking for; I define a varex environment for stating a variant of an example, computing its number via the label of the original one and a suffix that is, by default, a prime, but another one can be specified as optional argument.

\documentclass[letterpaper,12pt]{article} 

%Math symbol packages
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}

%Add theorem support
\usepackage{amsthm}

%Expanded references
\usepackage{refcount}

%Add several formatting packages
\usepackage{paralist}
\usepackage[colorlinks=true,hidelinks]{hyperref}
\usepackage{cleveref}
\crefdefaultlabelformat{[#2#1#3]}

%Define example
\theoremstyle{definition}
\newtheorem{ex}{Example}
\newcounter{Hex} % a new counter for hyperref

%Access package-internal code that uses @

\makeatletter
\g@addto@macro\ex{\stepcounter{Hex}} % use Hex for making anchors

% Define examples without linebreaks
\newenvironment{varex}[2][$'$]
  {\edef\theex{\getrefnumber{#2}#1}%
   \addtocounter{ex}{-1}
   \ex}
  {\endex}

\makeatother

%Define cref for Examples
  \crefname{ex}{example}{examples}
  \creflabelformat{ex}{(#2#1#3)}

\begin{document}
\begin{ex}\label{first}
An example.
\end{ex}

\begin{ex}\label{second}
Another one.
\end{ex}

\begin{varex}{first}\label{first'}
This is example 1$'$.
\end{varex}

\begin{varex}[$''$]{first}\label{first''}
This is example 1$''$.
\end{varex}

References:

\cref{first}

\cref{second}

\cref{first'}

\cref{first''}

\end{document}

enter image description here

share|improve this answer
    
This works great! It does, however, put linebreaks back in to my examples (they were, before, mentioned inline in the paragraph). Any chance I could still use the shortex format I defined in the original code by substituting shortex for ex in the relevant passages? –  djupp Mar 10 '13 at 17:36
    
I needed to reinsert the shortex environment in order to get the linebreaks back out, but other than that it does exactly what I wanted. Thanks so much (also on the help with properly formatting the question!) –  djupp Mar 10 '13 at 17:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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