4

I define an environment that changes the standard text shape (to \small and \itshape). I then add a table inside this environment and I would like it to have the same appearance as defined in the environment.

Is this possible without modifying the table separately? And if so, how? The same question would apply to a figure (or any other float) inside the environment. Also, it would be nice if the table would adjust to the smaller textwidth inside the environment (look at the table caption to see what I mean).

MWE:

\documentclass{article}

%define environment
\newcounter{examplecounter}
\newenvironment{example}{\small\begin{quote}%
  \refstepcounter{examplecounter}%
  \textbf{Example \arabic{examplecounter}}%
  \quad%
\itshape%
}{%
  \end{quote}%
}

\begin{document}
This is normal text. 
\begin{example}
  This is small and italics text. 

  \begin{table}[htp]
    \centering
      \begin{tabular}{lrr}
        \hline
        Parameters & Values & Description \\
        \hline
        $t$   & 2 & Time\\
        $c$   & 0.01 & Bond coupon\\
        \hline
      \end{tabular}
      \caption{This and the table contents should be small and italics
        as well.}
      \label{tab1}
  \end{table}   
\end{example}      
\end{document}

Output: Example

Jost
  • 1,337
  • 1
    the whole point of the table environment is that it may float to a different part of the document so it goes to some lengths not to pick up the surrounding environment. If you want a local table using the current settings why not just use tabular directly? – David Carlisle Sep 26 '16 at 17:19
  • Because I want the float to be able to float (especially inside the environment) and keep useful features such as caption and label. I just want it to visually match the environment. – Jost Sep 26 '16 at 17:22
  • if the table is this small, it may not be necessary to use the floating table environment. take a look at the linked answer to this question: How to influence the position of float environments like figure and table in LaTeX?. removing the floating aspect will automatically result in application of the local font size and text sidth. – barbara beeton Sep 26 '16 at 17:25
  • 2
    It would be easy enough to make it match the environment where it is saved (\let\@parboxrestore\relax\let\@floatboxreset\relax might be all you need) but it would be massively complicated to make a floating environment pick up the evironment of the page it floats to (you would need to re-write the entire float mechanism) so if you are OK with a float on page 10 using a narrow width and italic font because it was saved in an environment on page 2, I could make an answer, }% – David Carlisle Sep 26 '16 at 17:35
  • This will be a large technical document with many long tables of input and output variables, mixed with some numerical examples. And I want the reader to see immediately, if he's looking at example data or not. Where the table is floated to does not matter, all that matters is, whether it has been defined in the source code inside an example environment or not. @David your proposal sounds good to me. – Jost Sep 26 '16 at 18:10
  • 1
    But then you should define another float type (even if it is also labeled Table) with the characteristics that the example tables should have, and not make it dependent on where the table is located in the source. – gernot Sep 26 '16 at 18:16
  • @gernot That's an interesting idea. Could I define a new table that is used whenever it is found inside an example environment? I want to avoid to edit all relevant tables manually, especially since this is a multi-user project (many people edit the same document). – Jost Sep 26 '16 at 18:36
  • I have now added a solution where the same table code behaves differently, depending on whether it appears in an example environment or not. So none of the existing tables have to be rewritten. But it looks like an hack to me. As long as the number of existing tables is just a few dozens, I'd still rename them to another environment name, say extable, to separate clearly the two types. – gernot Sep 26 '16 at 19:22

3 Answers3

3

This variant redefines table such that it behaves normal except when used inside an example environment, where a small italics font is chosen. The justification for redefining table is that there are already lots of tables that should not be touched (see the discussion below the original post).

\documentclass{article}

\newcounter{examplecounter}
\newif\ifInExample
\newenvironment{example}%
  {\small\begin{quote}%
   \refstepcounter{examplecounter}%
   \textbf{Example \arabic{examplecounter}}%
   \quad
   \itshape
   \InExampletrue
  }%
  {\end{quote}%
  }

\makeatletter
\let\tableorig\table
\let\endtableorig\endtable
\renewenvironment{table}%
  {\ifInExample
     \let\@floatboxresetorig\@floatboxreset
     \def\@floatboxreset{\@floatboxresetorig\small\itshape}%
   \fi
   \begin{tableorig}%
  }%
  {\end{tableorig}%
  }
\makeatother

\begin{document}
This is normal text.

\begin{example}
  This is small and italics text. 
  \begin{table}[htp]
    \centering
    \begin{tabular}{lrr}
      \hline
      Parameters & Values & Description \\
      \hline
      $t$        &      2 &        Time \\
      $c$        &   0.01 & Bond coupon \\
      \hline
    \end{tabular}
    \caption{This and the table contents are small and italics.}
    \label{tab1}
  \end{table}   
\end{example}

\begin{table}[htp]
  \centering
  \begin{tabular}{lrr}
    \hline
    Parameters & Values & Description \\
    \hline
    $t$        &      2 &        Time \\
    $c$        &   0.01 & Bond coupon \\
    \hline
  \end{tabular}
    \caption{This and the table contents are normalsize and upright.}
  \label{tab2}
\end{table}
\end{document}

enter image description here

gernot
  • 49,614
3

This is certainly the wrong markup, floats by design are independent of the current environment, it would be better to use a command that set up the style for example floats wherever they were saved, but anyway if you disable the commands that normalize the environment, the current environment is picked up.

enter image description here

\documentclass{article}

\makeatletter
\def\hmmm{\let\@parboxrestore\relax\let\@floatboxreset\relax}
\makeatother

%define environment
\newcounter{examplecounter}
\newenvironment{example}{\small\begin{quote}%
  \refstepcounter{examplecounter}%
  \textbf{Example \arabic{examplecounter}}%
  \quad%
\itshape%
}{%
  \end{quote}%
}

\begin{document}
This is normal text. 
\begin{example}
  This is small and italics text. 
\hmmm
  \begin{table}[htp]
    \centering
      \begin{tabular}{lrr}
        \hline
        Parameters & Values & Description \\
        \hline
        $t$   & 2 & Time\\
        $c$   & 0.01 & Bond coupon\\
        \hline
      \end{tabular}
      \caption{This and the table contents should be small and italics
        as well.}
      \label{tab1}
  \end{table}   
\end{example}      
\end{document}

beware \@parboxrestore is also used in parboxes and table p columns, so if you do not want those to inherit the environment you might want to reset this command inside the table.

David Carlisle
  • 757,742
3

Instead of trying to shoehorn a non-floating table into the example environment, load the caption package and use the macro \captionof{table}{...} to create the table's caption. The caption package also lets you change the font size and shape of the caption. Separately, I suggest you use a centered minipage environment instead of a quote environment to contain the material of the example environment.

enter image description here

\documentclass{article}
\usepackage{caption} % for "\captionof" and "\captionsetup" macros
\usepackage{lipsum}  % for filler text
% Define "example" environment
\newcounter{examplecounter}
\newenvironment{example}{%
  \par
  \bigskip\noindent
  \refstepcounter{examplecounter}%
  \hspace{0.1\textwidth}%  % set indent and width 
  \begin{minipage}{0.8\textwidth} 
  \captionsetup{size=small,font=it}
  \textbf{Example \theexamplecounter}%
  \quad%
  \itshape% %font shape for remainder of "minipage" env.
}{%
  \end{minipage}
  \bigskip%
}

\begin{document}

\noindent
This is normal text. 
\begin{example}
  This is small and italic text. 
    \par\centering\medskip
      \begin{tabular}{lrr}
        \hline
        Parameters & Values & Description \\
        \hline
        $t$   & 2 & Time\\
        $c$   & 0.01 & Bond coupon\\
        \hline
      \end{tabular}
      \captionof{table}{This caption and the table contents 
      now are small and italic as well.}
      \label{tab1}
\end{example}      

\lipsum[2] % filler text
\end{document}
Mico
  • 506,678