r预订:PDF输出中的自定义块带有灰色背景和图标

时间:2019-11-05 18:06:49

标签: r latex bookdown

所以我花了几个小时试图找出一种方法,像在CSS的帮助下在html中一样,在LaTeX输出中获得带有图标的浅灰色自定义块。 LaTex代码很好用,因为它可以在最小的可重现示例中工作,但是当使用rMarkdown和bookdown包进行渲染时,它根本无法正确渲染。我最接近的是该块中的灰色背景,左侧的图标对齐错误,没有灰色背景,但是我想要的是:

enter image description here

这是一个简短的示例,说明它如何在普通LaTeX中工作。

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[british]{babel}
\usepackage[T1]{fontenc}
\usepackage{xcolor}
\definecolor{lightgray}{HTML}{f5f5f5}
\usepackage{adjustbox}
\usepackage{fontawesome}

\newenvironment{warning}[1]% #1 = text on left
{\noindent\begin{adjustbox}{minipage=\linewidth,margin=1ex,bgcolor=lightgray,env=center}
    \sbox0{#1}% measure width
    \usebox0\hfill% align baseline with center of minipage
    %\raisebox{\dimexpr 0.5\dp0-0.5\ht0}{\usebox0}\hfill% align center with center
    \begin{minipage}{\dimexpr \textwidth-\wd0-\columnsep}% added \columnsep gap between
}
{%
\end{minipage}\end{adjustbox}%
}

\begin{document}

\begin{warning}{\Huge \faWarning}
I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works. I hope it works.  
\end{warning}

\end{document}

由于rMarkdown存在xcolor的已知问题,因此可以像在this示例中那样轻松地将颜色更改为shadecolor,在该示例中,我获得了最接近的工作解决方案的代码。 。但是,当我在preamble.tex中呈现代码时,该块变得非常小,因此必须放大300倍才能读取内容。这是为什么?是什么原因使rMarkdown如此挑剔地允许编译哪个LaTeX代码?有谁知道如何像在HTML中那样工作?

相当于CSS的

div.warning {
  padding: 1em 1em 1em 5em;
  margin-bottom: 10px;
  background: #f5f5f5;
  position:relative;
}

div.warning:before {
  content: "\f071";
  font-family: FontAwesome;
  left:10px;
  position:absolute;
  top:5px center/3em no-repeat;
  font-size: 55px;
}

PS:下面的代码实际上可以在我的preamble.tex文件中运行,但没有灰色背景和错误的对齐图标:

\usepackage{booktabs}
\usepackage{amsthm}
\usepackage[ngerman]{babel}
\usepackage{fontawesome}

% larger font size for fontawesome icons
\makeatletter 
\renewcommand\Huge{\@setfontsize\Huge{30pt}{20}}
\makeatother

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Grey Blocks
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\makeatletter
\newenvironment{kframe}{%
    \medskip{}
    \setlength{\fboxsep}{.8em}
    \def\at@end@of@kframe{}%
    \ifinner\ifhmode%
    \def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
    \fi\fi%
    \def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
        \colorbox{shadecolor}{##1}\hskip-\fboxsep
        % There is no \\@totalrightmargin, so:
        \hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
    \MakeFramed {\advance\hsize-\width
        \@totalleftmargin\z@ \linewidth\hsize
        \@setminipage}}%
{\par\unskip\endMakeFramed%
    \at@end@of@kframe}
\makeatother

\makeatletter
\@ifundefined{Shaded}{
}{\renewenvironment{Shaded}{\begin{kframe}}{\end{kframe}}}
\makeatother
%           \raisebox{-.7\height}[0pt][0pt]{
\newenvironment{rmdblock}[1]
{
    \begin{itemize}
        \renewcommand{\labelitemi}{
            \raisebox{-0.9\height}[0pt][0pt]{
                {\setkeys{Gin}{width=3em,keepaspectratio}\Huge #1}
            }
        }
        \setlength{\fboxsep}{1em}
        \begin{kframe}
            \item
        }
        {
        \end{kframe}
    \end{itemize}
}
\newenvironment{warning}
{\begin{rmdblock}{\faWarning}}
    {\end{rmdblock}}

为您提供此图像 enter image description here

0 个答案:

没有答案