3

The question, How to embed a minted environment inside a tabular environment?, addresses a similar issue, but I am using:

  1. the tabularx package, and
  2. the inline \mint command

Is there a workaround available for this?

\documentclass[10pt]{article}
\usepackage[scaled]{beramono}
\usepackage[T1]{fontenc}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{minted}


\begin{document}

\begin{tabularx}{\linewidth}{@{}lrX}

Type & Meaning & Size \\
\cmidrule(r){1-1} \cmidrule(lr){2-2} \cmidrule(lr){3-3}

\mint{c++}|float| & single precision floating point  & 6 significant digits \\
            &               & Typically represented in 32 bits \\   

\mint{c++}|double| & double precision floating point  & 10 significant digits \\
            &               & Typically represented in 64 bits \\

\mint{c++}|long double| & extended precision floating point  & 10 significant digits \\
            &               & Typically represented in 96 or 128 bits \\        

\bottomrule                                                     

\end{tabularx}              

\end{document}
  • tabularx reads the table's contents as an argument, which limits the possibilities of including verbatim material. The package makes some adjustment in order to allow a restricted form of \verb, but doesn't cover other verbatim-like macros. – egreg May 29 '13 at 8:48
3

There are at least two ways to get this to work.

First method

Edit: For this approach to work, you will need to use the patch for \mint that is here.

Use \savebox. Before the tabularx, define something like the following for each of the terms you need.

\newsavebox{\floatinabox}
\begin{lrbox}{\floatinabox}
\mint{c++}|float|
\end{lrbox}

Then, inside the tabularx, use something like \usebox{\floatinabox}.

The lrbox typesets its contents in a box, and saves the box for later use rather than inserting the box in the document.

This same approach can be used for inserting a minted environment, except that a minipage is required to allow linebreaks (see this).

\newsavebox{\mintedbox}
\begin{lrbox}{\mintedbox}
\begin{minipage}{3cm}
\begin{minted}{c++}
long double
\end{minted}
\end{minipage}
\end{lrbox}

Second method

Use the Pygments highlighting from my pythontex package. The \pygment command takes the same arguments as \mint, and usually works in these types of situations as long as # and % aren't involved. I checked, and it does work in this case.

  • I've got two questions. One, why does the first method work? What makes the \mint command work normally inside an lrbox. Two, could you please add to the answer, what one should do to get the minted environment working inside a tabularx cell? – Chatterjee May 29 '13 at 10:05
  • @Chatterjee I've extended the answer. – G. Poore May 29 '13 at 11:06
  • Unfortunately, I'm getting a new error message now, saying Something's wrong--perhaps a missing \item – Chatterjee May 29 '13 at 18:51
  • 1
    Try using the patch for \mint here. Just copy everything from the \makeatletter through the \makeatother (including both of those) into your preamble. I was experimenting with using that patch when I answered the question, and hadn't realized that it seems to be needed. – G. Poore May 29 '13 at 19:21
  • 1
    @Chatterjee The answer is updated. The syntax highlighting part of pythontex is not a separate package because integrating the highlighting with code execution (pythontex's main purpose) is considerably more efficient overall. For future reference, I recently began maintaining minted. I will be updating it with several patches from this site and features from pythontex, so in the relatively near future you should be able to do everything with an updated version of minted. – G. Poore May 29 '13 at 20:00

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.