4

I have a though one for you:
I'm writing in MultiMarkdown and all my figures, tables, charts are usually pictures. This allows me to write something like this:

The MultiMarkdown input

![This is the caption.][table1]

[table_sometable]: tab_pic1.png

… and end up with:

Compiled LaTeX

\begin{figure}[htbp]
\centering
\includegraphics[keepaspectratio,width=\textwidth,height=0.75\textheight]{tab_pic1.png}
\caption{This is the caption.}
\label{table_sometable}
\end{figure}

I thought about writing a shell script that simple exchanges \begin{figure} to \begin{table} (same for \end{figure}) before I compile my .tex file with pdflatex to a PDF.

However, I'm looking for a pure LaTeX solution since it's more portable and more people at my University could use it.

After reading LaTeX programming: how to implement conditionals I thought that it might be possible and decided to ask my question here since I'm only 4 days old in the world of LaTeX.

I also read "How to form "if ... or ... then" conditionals in TeX?" and "LaTeX conditional expression", but I'm not sure if parsing for a string is supported.

I imagine the scipt to work like:

IF a string called \label{table_} is found between \begin{figure} and \end{figure} THEN replace {figure} with {table}

I also found Search & Replace Script for TeXworks but maybe it's possible to handle this all on the compile level, so that I don't have to run anything extra and just can start compiling in Sublime Text when I'm done writing.


So why would this be cool…

  • obviously it's kind of a must-have for people who write in (Multi)Markdown
  • a filter would also allow to split all figures further into catogories (photographies/images, charts, …)
  • other geeky stuff which depends on parsing the content before compiling a pdf.

I'm open for ideas, solutions, links.

3

Here is an implementation using environ and expl3. With environ we absorb the whole environment's contents and with l3regex we check whether the environment contains the tokens

\label{table_

via the regular expression

\c{label}\cB.table_

Where \c{<string>} matches the control sequence with name <string> and \cB. matches one open brace.

\documentclass{article}
\usepackage{environ,xparse,l3regex}

% environ has problems in renewing
% existent environments
\let\figure\relax
\let\endfigure\relax

\NewEnviron{figure}{\CheckForTable}

\makeatletter
\ExplSyntaxOn
\cs_new_protected:Npn \pattulus_check:n #1
 {
  \regex_match:nnTF { \c{label}\cB.table_ } { #1 }
   {
    \@float{table}#1\end@float
   }
   {
    \@float{figure}#1\end@float
   }
 }
\cs_generate_variant:Nn \pattulus_check:n { V }
\NewDocumentCommand{\CheckForTable}{}
 {
  \pattulus_check:V \BODY
 }
\ExplSyntaxOff
\makeatother

\begin{document}
\begin{figure}[htbp]
\centering
THIS SHOULD BE A TABLE
\caption{This is the caption.}
\label{table_sometable}
\end{figure}

\begin{figure}[htbp]
\centering
THIS SHOULD BE A FIGURE
\caption{This is the caption.}
\label{figure_somefigure}
\end{figure}

\end{document}

enter image description here

Update March 2019

After several updates of expl3 and xparse, the code can be reduced:

\documentclass{article}
\usepackage{xparse}

\makeatletter
\ExplSyntaxOn

% use the default placements htp
\RenewDocumentEnvironment{figure}{O{htp} +b}
 {\pattulus_check:nn { #1 } { #2 }}
 {}

\cs_new_protected:Nn \pattulus_check:nn
 {
  \regex_match:nnTF { \c{label}\cB.table_ } { #2 }
   {
    \@float{table}[#1]#2\end@float
   }
   {
    \@float{figure}[#1]#2\end@float
   }
 }

\ExplSyntaxOff
\makeatother

\begin{document}
\begin{figure}
\centering
THIS SHOULD BE A TABLE
\caption{This is the caption.}
\label{table_sometable}
\end{figure}

\begin{figure}
\centering
THIS SHOULD BE A FIGURE
\caption{This is the caption.}
\label{figure_somefigure}
\end{figure}

\end{document}
  • This works like a charm. Thank you so much. Great to know how to use a regex with LaTeX. – pattulus Nov 29 '13 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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