GeneralRefining environments through a macro

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
erwann
Posts: 75
Joined: Thu Aug 25, 2016 2:24 am

Refining environments through a macro

Post by erwann »

Hi,

a method for "defining a theorem environment which has a custom numbering scheme" was proposed here. I'm looking for a way to automate the creation of such custom theorems, such as follows:

Code: Select all

\metathmcustnum{foo}{Foo} 
\begin{foo}{8}Whatever\end{foo}
Output:
Foo 8
Whatever
x_86 / Linux Mint 18.3 / texlive 2015.20160320-1ubuntu0.1 / TeXworks 0.5r1361 (Debian)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

rais
Posts: 419
Joined: Sun Nov 16, 2014 8:51 pm

Refining environments through a macro

Post by rais »

here's an idea:

Code: Select all

\documentclass{article}
%% original by esdd, posted on http://tex.stackexchange.com/questions/53978/custom-theorem-numbering/53980
%% modified to be used through a wrapper command.
%% All modifications were marked with `mod:', all additions with `new:', except for pure comment lines (like this one), or an occasional empty line.

%\usepackage{amsthm} %% uncomment to see the difference

\makeatletter% new: the wrapper command created below will use commands with `@' in their names
\newcommand*\metathmcustnum[2]{% new: the wrapper command
  \newtheorem{inner@cust@#1}{#2}% mod: using the wrapper's arguments to build the `inner' theorem. Since the name for this inner theorem is now generated, I added "@cust@" to this name to prevent possible clashes with other packages---who knows, if \innersomething isn't already defined somewhere? 
  \newenvironment{#1}[1]% mod: the wrapper's first argument will be the name of the new environment
    {\@namedef{theinner@cust@#1}{##1}% mod: the counter name for the inner theorem needs to be constructed, first. That's where \@namedef comes in handy...
    % because \newenvironment is now called from within a command, the new environment's first (here only) argument is referred to as `##1' (`#1' refers to the warpper's first argument) 
      \@nameuse{inner@cust@#1}% mod: the name of the inner theorem needs to be constructed, before it can be called...an alternate may be `\csname inner@cust@#1\endcsname'
    }
    {\@nameuse{endinner@cust@#1}}% mod: same for the closing of the inner theorem
}
\makeatother% new: the closing command to \makeatletter

\metathmcustnum{customthm}{Theorem}% new: the original definition redone with the wrapper
\metathmcustnum{foo}{Foo}% new: additional definition
\begin{document}

\begin{customthm}{8}\label{eight}
Every theorem must be numbered by hand.
\end{customthm}

Here is a reference to theorem~\ref{eight}.

\begin{foo}{12}whatever\end{foo}% new: test additional environment
\end{document}
KR
Rainer
Post Reply