Text FormattingText Section in colored Box with numbered Lines

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
Klara
Posts: 2
Joined: Mon Jul 23, 2012 10:39 am

Text Section in colored Box with numbered Lines

Post by Klara »

Hi!

Firstly, before posting my LaTeX code I will explain, what I want to have.

In one chapter I am introducing some kind of pseudo-code. The pseudo-code should be within a gray box and every line should have a number. Then a description is following, outside the gray box and without line numbers and then the next lines of pseudo-code are following (again in a gray box). The second pseudo-code example should continue with the line numbers, according to the first one. My problem, I have either a gray box or the line numbers. I tried it like that:

Code: Select all

\colorbox{hellgrau{
  \parbox{\columnwidth}{                             
    \textbf{pseudo-code 1}\par
    \begin{linenumbers}
      \textbf{create} text\par
      \textbf{for each}text \newline
      text \newline
      text \newline
    \end{linenumbers}
  }
}
Then I have a gray box but no numbers and when I put the \colorbox within the linenumbers environment, the gray box is interpreted as one line. I already tried {minipage}, but then the line numbers are not working.

I am thankful for every advice!
Last edited by localghost on Mon Jul 23, 2012 11:21 am, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Text Section in colored Box with numbered Lines

Post by cgnieder »

Maybe instead of defining things for yourself you might want to consider using ready made solutions from packages like algorithms, algorithmicx and mdframed:

Code: Select all

\documentclass{scrartcl}
\usepackage{xcolor}% Farben
\usepackage{algorithm}% http://ctan.org/pkg/algorithms
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{mdframed}% Hintergrundbox, Rahmen, ...
\usepackage{lipsum}% für Dummy-Text

\colorlet{hellgrau}{gray!25}
\colorlet{pseudocodebg}{hellgrau}

\surroundwithmdframed[
  backgroundcolor   = pseudocodebg ,
  hidealllines      = true,
  innerleftmargin   = 0pt,
  innerrightmargin  = 0pt,
  innertopmargin    = 0pt,
  innerbottommargin = 0pt,
  skipabove         = .5\baselineskip,
  skipbelow         = .5\baselineskip
]{algorithmic}

\newmdenv[
  backgroundcolor   = white ,
  hidealllines      = true,
  innerleftmargin   = 1em ,
  innertopmargin    = .5\baselineskip,
  innerbottommargin = .5\baselineskip,
  leftmargin        = -\parindent
]{commentbox}

\algrenewcommand\algorithmiccomment[1]{\begin{commentbox}#1\end{commentbox}}
\newcommand*\var[1]{{\ttfamily#1}}
\begin{document}

\lipsum[1]
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b}
  \State $r\gets a\bmod b$ and \var{foobar}\label{foobar}
  \While{$r\not=0$}\Comment{We have the answer if r is 0}
    \State $a\gets b$
    \State $b\gets r$
    \State $r\gets a\bmod b$
  \EndWhile\label{euclidendwhile}
  \State \textbf{return} $b$\Comment{The gcd is b}
  \EndProcedure
\end{algorithmic}
Variable \var{foobar} (in line~\ref{foobar}), corresponds to\ldots

\end{document}
pseudocode.png
pseudocode.png (28.52 KiB) Viewed 4094 times
Regards
site moderator & package author
Klara
Posts: 2
Joined: Mon Jul 23, 2012 10:39 am

Text Section in colored Box with numbered Lines

Post by Klara »

Thanks for your answer. I already considered that packages but it is not possible to use them for my constructs... so for example I use commands defined by my own, like:

Code: Select all

\textbf{connect} ELEMENT (xxx) \textbf{with} ELEMENT (yyy)
or if-then-else statements like

Code: Select all

\textbf{if} ELEMENT (xxx) \textbf{is not assigned to} ELEMENT (yyy) \textbf{then} ...
For sure it would be nicer with an algorithmic package, but as I found out, my pseudo-code is not fitting that constraints.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Text Section in colored Box with numbered Lines

Post by cgnieder »

Could you then provide a complete Infominimal working example, please? That would help very much for proposing a proper solution suiting your needs.

BTW: your previous post does not show commands defined by you but only \textbf (which I would expect being part of custom commands but not being used directly).

Regards
site moderator & package author
Post Reply