6

Background

A number of source files are included within floating algorithm boxes. The source files are PNG images with a transparent background. The document is edited using LyX.

Problem

The source code listings (algorithms) are applied to the document as follows:

% Change the background colour of algorithm boxes.
\let\oldalgorithm\algorithm
\let\endoldalgorithm\endalgorithm
\renewenvironment{algorithm}[1][htbp]{
  \let\graphicsformat\justifiedandcolored
  \oldalgorithm[#1]
}%
  {\endoldalgorithm}

% Change the background colour of the algorithm floats.
\def\justifiedandcolored#1{%
  \setlength\fboxrule{0pt}%
  \setlength\fboxsep{0pt}%
  \kern-1.35pt
  \colorbox{sourcecolour}{%
    \hbox to\linewidth{#1}%
  }%
  \par
  \kern-1.5pt
}

I would like to change the colorbox to a gradient (rather than a solid colour), using the pst-grad package or perhaps the tikz package.

All the examples I could find for pst-grad show how to apply a gradient to a box with known dimensions. In this situation, the width and height are unknown. The dimensions depend on:

  1. The resolution of the image.
  2. The scaling of the image (large images are scaled down to the page margins).

Example

Here is a small source document:

% Preview source code

%% LyX 1.6.7 created this file.  For more info, see http://www.lyx.org/.
%% Do not edit unless you really know what you are doing.
\documentclass[letterpaper,oneside,english,twoside,pointlessnumbers,obeyspaces,plainpages=false,pdfpagelabels ]{scrbook}
\usepackage{lmodern}
\usepackage{helvet}
\usepackage{courier}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\setcounter{secnumdepth}{3}
\setcounter{tocdepth}{3}
\usepackage{babel}

\usepackage{pifont}
\usepackage{float}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{amssymb}
\usepackage[unicode=true, pdfusetitle,
 bookmarks=true,bookmarksnumbered=false,bookmarksopen=false,
 breaklinks=false,pdfborder={0 0 1},backref=false,colorlinks=false]
 {hyperref}

\makeatletter

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% LyX specific LaTeX commands.
\special{papersize=\the\paperwidth,\the\paperheight}

%% A simple dot to overcome graphicx limitations
\newcommand{\lyxdot}{.}

\floatstyle{ruled}
\newfloat{algorithm}{tbp}{loa}[chapter]
\floatname{algorithm}{Algorithm}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% User specified LaTeX commands.
\input{example-preamble.tex}

\AtBeginDocument{
  \def\labelitemi{\ding{233}}
  \def\labelitemii{\ding{219}}
  \def\labelitemiii{\normalfont\bfseries{--}}
}

\makeatother

\begin{document}
\input{chapter.tex}


\chapter{Chapter}

%
\begin{algorithm}[H]
\includegraphics{source/sql/beautify-after\lyxdot sql}

\caption{\label{alg:After-Beautification}After Beautification}

\end{algorithm}

\end{document}

Here is the example-preamble.tex:

\usepackage{algorithm}
\usepackage{caption}
\usepackage{float}
\usepackage{graphicx}
\usepackage[T1]{fontenc}
\usepackage{mathptmx}
\usepackage[automark,nouppercase]{scrpage2}
\usepackage[dvipsnames,svgnames,x11names,table]{xcolor}

% Use a hyphen for captions, and make links give a bit of space.
\captionsetup{hypcapspace=0.5\baselineskip}
\captionsetup{labelfont=bf,labelsep=endash}
\captionsetup[ruled]{labelfont=bf,labelsep=endash}

% Change algorithm to "Listing".
\floatname{algorithm}{Listing}
\newcommand{\algorithmname}{Listing}

% Colour definitions.
\input{colours.tex}

% Determine if the image is too wide for the page.
\makeatletter
\def\ScaleIfNeeded{%
  \ifdim\Gin@nat@width>\linewidth
    \linewidth
  \else
    \Gin@nat@width
  \fi
}
\makeatother

% Resize figures that are too wide for the page.
\let\oldincludegraphics\includegraphics
\renewcommand\includegraphics[2][]{%
  \graphicsformat{%
    \oldincludegraphics[width=\ScaleIfNeeded]{#2}%
  }%
}

% Centre graphics within non-Algorithm floats.
\let\graphicsformat\centering

% Change the background colour of algorithm boxes.
\let\oldalgorithm\algorithm
\let\endoldalgorithm\endalgorithm
\renewenvironment{algorithm}[1][htbp]{
  \let\graphicsformat\justifiedandcolored
  \oldalgorithm[#1]
}%
  {\endoldalgorithm}

% Change the background colour of the algorithm floats.
\def\justifiedandcolored#1{%
  \setlength\fboxrule{0pt}%
  \setlength\fboxsep{0pt}%
  \kern-1.35pt
  \colorbox{sourcecolour}{%
    \hbox to\linewidth{#1}%
  }%
  \par
  \kern-1.5pt
}

% Required by LyX...
\makeatletter

Example image:

enter image description here

Example PDF:

enter image description here

Question

What is the best way to replace colorbox with a gradient?

Ideas

One idea I had was to use ImageMagick on the image and add a gradient that way. If I decide to change the gradient colours, I'd have to regenerate dozens of source files. This solution would work, but is not ideal.

Thank you!

lockstep
  • 250,273
Dave Jarvis
  • 11,809
  • 1
    can you provide a complete small example? –  Feb 11 '11 at 21:05
  • ... with a picture uploaded somewhere that we can use. – Caramdir Feb 11 '11 at 21:30
  • From your description, it seems to me that putting the picture into a tikz node, like in this question: http://tex.stackexchange.com/questions/9559/drawing-on-an-image-with-tikz, except that you would draw the gradient under the picture rather than over it, may work for you. I am not sure it will work with png images. I am actually surprised that what you describe works for you, I thought that tex did not support transparency in png images. – Jan Hlavacek Feb 12 '11 at 01:00
  • Apparently, my information about tex not supporting transparency in png was outdated. It works very nicely! – Jan Hlavacek Feb 12 '11 at 14:15
  • 1
    Dave, please always use the default "Add Image" interface to ensure that the images are here to stay. Your images seem to be broken because of the automatic URL replacement. See How do we repair Gonzalo Medina's posts containing images? for more information. – Martin Scharrer Jul 04 '11 at 11:31
  • Why are the source files images rather than just text in a listings or algorithm environment? – Seamus Aug 24 '11 at 15:28
  • listings or algorithm have insufficiently flexible syntax highlighting. See also: http://superuser.com/questions/213217/convert-html-to-image and the image response to the accepted answer. LaTeX could, with perhaps a year of my effort, produce the syntax highlighting in all the languages I needed. Or I could write a simple script to take gVim's HTML output and create an image to embed in only several hours' worth of effort. – Dave Jarvis Aug 25 '11 at 02:34
  • @Dave Jarvis, I get "! TeX capacity exceeded, sorry [input stack size=5000]." when I try to use your colored algorithm environment. Any suggestions? – Alec Jacobson Mar 29 '13 at 01:15
  • @mangledorf: Ask a new question on here and show the full preamble that you're using. – Dave Jarvis Mar 29 '13 at 01:16

1 Answers1

11

You can use tikz with the backgrounds library for this. You need to load tikz and the backgrounds library after the xcolor package, so this would have to be placed in your example-preamble.tex

\usepackage{tikz}
\usetikzlibrary{backgrounds}

In your example-preamble.tex, change the definition of \justifiedandcolored to this:

\def\justifiedandcolored#1{%
  \setlength\fboxrule{0pt}%
  \setlength\fboxsep{0pt}%
  \tikz[background rectangle/.style={top color=red!20,bottom color=white},
  tight background,
  show background rectangle] 
  \node [inner sep=0pt] (0,0) {#1};%
}

Which will yield the following output:

enter image description here

Jake
  • 232,450