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’m trying to align verbatim segments with their outputs. Consider this MWE:

\documentclass{article}
\usepackage{lmodern}
\usepackage{fancyvrb}
\DefineVerbatimEnvironment{IVerbatim}{Verbatim}{xleftmargin=2em, gobble=2}

\begin{document}
Here is some verbatim text, indented by 2em:

\begin{IVerbatim}
  Do \textit{something}!
\end{IVerbatim}

Now here is its result, indented by 2em:

\begin{quote}
  Do \textit{something}!
\end{quote}

How can I get them to align?    
\end{document}

Both the quote & the IVerbatim environments are indented by 2em; the trouble is that the em of the LMRoman font is wider than the em of the LMMono font.

(Correction: As mentioned in the accepted answer, the quote environment is actually indented by 2.5 em.)

How can I set the indent of my IVerbatim segments to be 2em of the roman font? This needs to be something I can plug into the line xleftmargin=〈something.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

The value of the quad width (=1em) is controlled by \fontdimen6. According to article.cls, the indentation used inside a quote environment is 2.5em, so you can say something like the following:

\documentclass{article}
\usepackage{lmodern}
\usepackage{fancyvrb}
\usepackage{tikz}
\usetikzlibrary{calc}
\DefineVerbatimEnvironment{IVerbatim}{Verbatim}{xleftmargin=2.5\mylen, gobble=2}

\newlength\mylen

\begin{document}
\setlength\mylen{\fontdimen6\font}
The value of \texttt{1em} is: \the\mylen

Here is some verbatim text, indented by 2.5em:
\begin{IVerbatim}
  Do \textit{something}!
\end{IVerbatim}

Now here is its result, indented by 2.5em:
\begin{quote}
Do \textit{something}!
\end{quote}

\tikz[remember picture,overlay] \draw[blue] ( $ (current page.north west) + (159.5pt,0) $ )-- ( $ (current page.south west)+ (159.5pt,0) $ );    

\end{document}

I only added the vertical rule for visualization purposes:

enter image description here

Another way to get the value for 1em would be by measuring the length of a \quad space:

\settowidth\mylen{\quad}\the\mmylen

Here's a simple document showing the different values for the quad width depending ob the font family for the lmodern fonts:

\documentclass{article}
\usepackage{lmodern}

\setlength\parindent{0pt}
\newcommand\QuadWd{%
  The value of 1em is \the\fontdimen6\font\par}

\begin{document}

\QuadWd
{\ttfamily\QuadWd}
{\sffamily\QuadWd}

\end{document}

enter image description here

share|improve this answer
    
Is there some way to do this as xleftmargin=2.5\fontdimen6\rmfont or something? –  J. C. Salomon Aug 13 '12 at 1:08
    
Suggested improvement: Together with the environment definition and the newlength declaration, put the line \AtBeginDocument{\setlength\mylen{\fontdimen6\font}}. Keeps logically-related things together. I’ve implemented this for my xpeek package in this commit. –  J. C. Salomon Aug 13 '12 at 18:55

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.