The problem with your code is that the width of the table generated by the tabular environment is lesser than \textwidth. Instead of solving that, I would suggest playing with the fancyvrb package, which allows to define custom verbatim environments. Please consider the following code:
Code: Select all
\documentclass[a4paper,12pt]{article}
\usepackage{fancyvrb,color}
\makeatletter
\newcounter{ruleverb}
\newcommand\FVB@RuleVerbatim{\refstepcounter{ruleverb}%
\par\vspace{\abovedisplayskip}\noindent%
\FV@BVerbatimBegin\FV@Scan}
\newcommand\FVE@RuleVerbatim{\FV@BVerbatimEnd\hfill[Rule~\theruleverb]%
\par\vspace{\belowdisplayskip}}
\DefineVerbatimEnvironment{RuleVerbatim}{RuleVerbatim}{baseline=c}
\define@key{FV}{reflabel}{%
\def\@tempa{#1}\ifx\@tempa\empty\relax\else\label{#1}\fi}
\makeatother
\begin{document}
This is some text that precedes the numbered verbatim text that I'd like to use
\begin{RuleVerbatim}
private string testToken;
private int testInt;
other line of code;
and another one
\end{RuleVerbatim}
This text is just following some rule text to fill in the margins and stuff.
Bla bla bla bla
This is some text that precedes the numbered verbatim text that I'd like to use
\begin{RuleVerbatim}[gobble=5]
private string testToken;
private int testInt;
other line of code;
and another one
\end{RuleVerbatim}
This text is just following some rule text to fill in the margins and stuff.
Bla bla bla bla
This is some text that precedes the numbered verbatim text that I'd like to use
\begin{RuleVerbatim}[reflabel=rulex]
private string testToken;
private int testInt;
other line of code;
and another one
\end{RuleVerbatim}
This text is just following Rule~\ref{rulex} text to fill in the margins and stuff.
Bla bla bla bla
This is some text that precedes the numbered verbatim text that I'd like to use
\begin{RuleVerbatim}[reflabel=ruley,gobble=3,fontsize=\footnotesize]
private string testToken;
private int testInt;
other line of code;
and another one
\end{RuleVerbatim}
This text is just following Rule~\ref{ruley} text to fill in the margins and stuff.
Bla bla bla bla
This is some text that precedes the numbered verbatim text that I'd like to use
\begin{RuleVerbatim}[reflabel=rulez,fontsize=\small,formatcom=\color{red}]
private string testToken;
private int testInt;
other line of code;
and another one
\end{RuleVerbatim}
This text is just following Rule~\ref{rulez} text to fill in the margins and stuff.
Bla bla bla bla
\end{document}
The RuleVerbatim environment works as any other one defined in fancyvrb. Read the
package manual. However, since RuleVerbatim is based on BVerbatim (cf. subsection 4.2 of the manual) some parameters does not work (frame, numbers...). Likewise, to cite some instance of RuleVerbatim, use the new parameter reflabel (defined in the preamble of the above code). Finally, the space above and below the environment is that of a usual displayed equation (\abovedisplayskip and \belowdisplayskip).
I hope the code may help.