Text FormattingLabeled verbatim

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
mobow
Posts: 4
Joined: Wed Jul 01, 2009 10:16 am

Labeled verbatim

Post by mobow »

Dear All,

I have a problem with the formatting of my document. I need a (multilined) verbatim to be labled just like an equation, so I could cross-reference it in the text. The verbatim text should be on the left and the number (like [chapternumber].[verbatimnumber]) on the right (just like an equation).

I have tried to create a new environment and creating a table with in the left column the verbatim text and in the right column the number, however I don't get this to work.

I have also looked for a different approach, like a multiline equation in verbatim style, however I couldn't find any.

edit:

I now have this

Code: Select all

\newcounter{ruleverb}
\newenvironment{ruleverb}
	{\refstepcounter{ruleverb}
          \begin{tabular}{m{0.8\textwidth} r}}
     { & 
     Rule~\theruleverb %
     \end{tabular}}
However it does not take into account the margin of the text.

Thank you in advance!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Re: Labeled verbatim

Post by localghost »

If you could explain a little bit more concrete what you are aiming at, it would be easier to give some suggestions. From your code sample I only can see that this should be called a "Rule" but I do not yet understand what purpose is behind the verbatim part (maybe a kind of theorem or listing or something similar). You may describe the look of the final output apart from the equation like numbering.


Best regards and welcome to the board
Thorsten
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Labeled verbatim

Post by frabjous »

Perhaps this thread may provide some guidance.
mobow
Posts: 4
Joined: Wed Jul 01, 2009 10:16 am

Labeled verbatim

Post by mobow »

Thank you both for your reaction. I have had a look at that page frabjous pointed to, but it was not exactly I was looking for. I think I now have something I am pretty satisfied with. One more thing is in the example I gave, how do I let the rule number be preceded by the chapter number? The final result must look something like this:
latex.jpg
latex.jpg (21.22 KiB) Viewed 5962 times
As you can see, my little dissatisfaction is in the fact that the 5 is not fully on the right margin.
Last edited by mobow on Thu Jul 02, 2009 11:29 am, edited 1 time in total.
User avatar
localghost
Site Moderator
Posts: 9201
Joined: Fri Feb 02, 2007 12:06 pm

Labeled verbatim

Post by localghost »

mobow wrote:[…] I think I now have something I am pretty satisfied with. One more thing is in the example I gave, how do I let the rule number be preceded by the chapter number? […]
Introduce the code for this layout to make others see what has to be modified. Please upload picture attachments to the forum server.
How to make a "Minimal Example"
Board Rules
Avoidable Mistakes[/size]

¹ System: openSUSE 42.2 (Linux 4.4.52), TeX Live 2016 (vanilla), TeXworks 0.6.1
mobow
Posts: 4
Joined: Wed Jul 01, 2009 10:16 am

Labeled verbatim

Post by mobow »

Again, the code is this:

Code: Select all

\newcounter{ruleverb}
\newenvironment{ruleverb}
	{\refstepcounter{ruleverb}
	\begin{tabular}{m{0.8\textwidth} r}}
     { & 
     Rule~\theruleverb %
     \end{tabular}}
To be used like this:

Code: Select all

\begin{ruleverb}
\label{rulex}
\begin{verbatim}
private string testToken;
private int testInt;
\end{verbatim}
\end{ruleverb}
Result is thus:
latex.jpg
latex.jpg (21.22 KiB) Viewed 5962 times
Sorry for not being clear the first time.
User avatar
Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

Labeled verbatim

Post by Juanjo »

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.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
mobow
Posts: 4
Joined: Wed Jul 01, 2009 10:16 am

Re: Labeled verbatim

Post by mobow »

Thank you! That was pretty much what I was looking for! :D

The only sad thing is that my editor (TeXlipse) does not like the verbatim that is not 'really' within a verbatim environment (e.g., does not start and end with verbatim). So all following text is no longer correctly colored. But I guess I have to live with it.
Post Reply