将渐变(阴影)背景应用于算法列表

将渐变(阴影)背景应用于算法列表

背景

浮动算法框内包含多个源文件。源文件是具有透明背景的 PNG 图像。该文档使用 LyX 进行编辑。

问题

源代码清单(算法)应用于文档如下:

% 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
}

我想使用包或者包将其更改colorbox为渐变色(而不是纯色)。pst-gradtikz

我能找到的所有示例都pst-grad展示了如何将渐变应用于已知尺寸的框。在这种情况下,宽度和高度未知。尺寸取决于:

  1. 图像的分辨率。
  2. 图像的缩放比例(大图像缩小到页边距)。

例子

这是一个小的源文档:

% 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}

以下是 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

示例图片:

在此处输入图片描述

PDF 示例:

在此处输入图片描述

问题

colorbox用渐变替换的最佳方法是什么?

想法

我的一个想法是在图像上使用 ImageMagick 并以此方式添加渐变。如果我决定更改渐变颜色,我必须重新生成数十个源文件。这个解决方案可行,但并不理想。

谢谢你!

答案1

您可以使用tikzbackgrounds来实现这一点。您需要在包之后加载库tikz,因此必须将其放在您的backgroundsxcolorexample-preamble.tex

\usepackage{tikz}
\usetikzlibrary{backgrounds}

在您的 中example-preamble.tex,将 的定义更改\justifiedandcolored为:

\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};%
}

这将产生以下输出:

在此处输入图片描述

相关内容