Text FormattingHave a grayed box that fits in a multi column.

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
csmgroup
Posts: 25
Joined: Sat Jul 17, 2010 6:58 pm

Have a grayed box that fits in a multi column.

Post by csmgroup »

This is the code that I experimented to have a grayed color box using colobox and tikzpicture. And I attach the result.

Code: Select all

\documentclass[]{article}
\usepackage{layouts}
\usepackage{multicol}
\usepackage{tikz}
\usepackage{color}
\usepackage{listings}

\usepackage[left=20mm,right=20mm,top=33.95mm,bottom=33.95mm]{geometry}

\begin{document}
textwidth in cm: \printinunitsof{cm}\prntlen{\textwidth}
textwidth in cm: \printinunitsof{cm}\prntlen{\linewidth}

\colorbox{darkgray}{Black text on red background}

\noindent\colorbox{darkgray}{\parbox{\linewidth}{Black text on red background}}

\begin{multicols}{2}


This one is deep. Remember etaoin shrdlu? (Maybe you don't. Never mind, a bulletin on the subject is forthcoming.) Remember the Illuminati and fnord? Lorem ipsum is the same deal--one of those inscrutable phrases that just keeps turning up. Surely it means something. Surely it's invested with, you know, mysto power. Lorem ipsum, my children. So mote it be.

textwidth in cm: \printinunitsof{cm}\prntlen{\textwidth}
textwidth in cm: \printinunitsof{cm}\prntlen{\linewidth}

This one is deep. Remember etaoin shrdlu? (Maybe you don't. Never mind, a bulletin on the subject is forthcoming.) Remember the Illuminati and fnord? Lorem ipsum is the same deal--one of those inscrutable phrases that just keeps turning up. Surely it means something. Surely it's invested with, you know, mysto power. Lorem ipsum, my children. So mote it be.


\noindent\colorbox{darkgray}{Black text on red background}

\noindent\colorbox{darkgray}{\parbox{\linewidth}{Black text on red background}}

\definecolor{darkgray}{rgb}{0.95,0.95,0.95}
\lstset{backgroundcolor=\color{darkgray}}
\lstset{columns=fullflexible, basicstyle=\ttfamily,  basicstyle=\tiny, numbers=left, numberstyle=\tiny, stepnumber=2, numbersep=5pt}
\begin{lstlisting}[frame=tb]
(let ((buffer (url-retrieve-synchronously
	       "http://tromey.com/elpa/package-install.el")))
  (save-excursion
    (set-buffer buffer)
    (goto-char (point-min))
    (re-search-forward "^$" nil 'move)  ;;$
    (eval-region (point) (point-masx))
    (kill-buffer (current-buffer))))
\end{lstlisting}

\end{multicols}

\end{document}
Compared to the listing environment, the two gray boxes are not aligned to the rightmost line.

I used \textwidth, but it doesn't give me the correct width, so I used \linewidth with \noindent. But, the result is not correct.

What went wrong? What value should I give for text width to get everything correct?
Attachments
Screen shot 2010-07-18 at 4.07.36 PM.png
Screen shot 2010-07-18 at 4.07.36 PM.png (62.82 KiB) Viewed 4256 times
Last edited by csmgroup on Tue Jul 20, 2010 1:42 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.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Have a grayed box that fits in a multi column.

Post by gmedina »

Hi,

you have to take into account the length \fboxsep (default value: 3pt) which controls the space between the edge of the box and its contents; since this length appears twice (once at the beginning and once at the end), you have to subtract 6pt to \textwidht.

Compare the results in this example:

Code: Select all

\documentclass{article}
\usepackage[left=20mm,right=20mm,top=33.95mm,bottom=33.95mm]{geometry}
\usepackage{xcolor}

\newlength\mylen
\setlength\mylen{\textwidth}
\addtolength\mylen{-6pt}

\begin{document}

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

\noindent\colorbox{darkgray}{\parbox{\linewidth}{Black text on red background}}

text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text text

\noindent\colorbox{darkgray}{\parbox{\mylen}{Black text on red background}}

\end{document}
Perhaps the framed environment (from the framed package) could be of interest to you.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
csmgroup
Posts: 25
Joined: Sat Jul 17, 2010 6:58 pm

Re: Have a grayed box that fits in a multi column.

Post by csmgroup »

Yes, it seems OK. Thanks for the help.
Post Reply