简体   繁体   English

Rmarkdown 中 TOC 上的图像徽标

[英]image logo over TOC in Rmarkdown

I know I can insert a logo or image at the top of HTML report rendered using knitr with in_header/before_body options我知道我可以在使用带有in_header/before_body选项的 knitr 呈现的 HTML 报告的顶部插入徽标或图像

output:
  html_document:
    includes:
      before_body: header.Rhtml

My guess is: how to render logo over floating TOC?我的猜测是:如何在浮动目录上渲染徽标?

output:
  html_document:
    toc: true
    toc_float: true
    collapsed: false
    ??????

You got a few options here and I will outline two of them 你在这里有几个选择,我将概述其中两个

1. Use the CSS pseudo element before 1.使用CSS伪元素before

The following snippet will add the stackoverflow logo just above the first TOC element and within the TOC box: 以下代码段将在第一个TOC元素上方和TOC框中添加stackoverflow徽标:

<style>
#TOC {
  background: url("https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a");
  background-size: contain;
  padding-top: 80px !important;
  background-repeat: no-repeat;
}
</style>

Here you will have to adjust the padding-top definition depending on your logo. 在这里,您必须根据您的徽标调整padding-top定义。 The result looks more or less like this: 结果看起来或多或少像这样:

在此输入图像描述

2. Add a new DOM element inside the TOC column 2.在TOC列中添加新的DOM元素

Another way is to use jQuery first in order to add a new DOM element that will contain the image: 另一种方法是首先使用jQuery来添加一个包含图像的新DOM元素:

<script>
  $(document).ready(function() {
    $('#TOC').parent().prepend('<div id=\"nav_logo\"><img src=\"https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a\"></div>');
  });
</script>

Here, when the document has finished loading, we select the element with the id TOC , then move up to its parent element (the column div ) and then prepend a new div with the id nav_logo containing the image. 这里,当文档加载完毕后,我们选择id为TOC的元素,然后向上移动到其父元素(列div ),然后在其中nav_logo包含图像的id为nav_logo的新div Sounds more complicated than it actually is. 听起来比实际更复杂。 Now we only have to edit the styles for this new div with some CSS again: 现在我们只需要再次使用一些CSS编辑这个新div的样式:

<style>
#nav_logo {
  width: 100%;
  margin-top: 20px;
}
</style>

Here the resulting document looks like this: 这里生成的文档如下所示:

在此输入图像描述

For details on CSS I would refer you to the big search engines which in turn will most probably refer you to https://www.w3schools.com . 有关CSS的详细信息,我会向您推荐大型搜索引擎,而这些搜索引擎很可能会将您推荐给https://www.w3schools.com

If I would have to pick, I would go with the second attempt. 如果我必须选择,我会进行第二次尝试。 Pseudo elements are not always reliable across browsers. 伪元素在浏览器中并不总是可靠的。 And it also looks better :) 它看起来也更好:)

To enable a logo that floats with TOC during scrolling in solution (#2) from @Martin Schmelzer prepend nav_logo DOM element containing the image to TOC using jQuery:要在@Martin Schmelzer的解决方案 (#2) 中滚动时启用随 TOC 浮动的徽标,请使用 jQuery 将包含图像的nav_logo DOM 元素添加到TOC

<script>
  $(document).ready(function() {
    $('#TOC').parent().prepend('<div id=\"nav_logo\"><img src=\"https://cdn.sstatic.net/Sites/stackoverflow/company/img/logos/so/so-logo.png?v=9c558ec15d8a\"></div>');
  });
</script>

And then add position: fixed to nav_logo and margin-top: 100px to TOC in CSS:然后在 CSS 中的TOC中添加position: fixed to nav_logomargin-top: 100px

<style>
#TOC {
  margin-top: 100px;
}
#nav_logo {
  position: fixed;
  width: 20%;
  margin-top: 20px;
}
</style>

Adjust nav_logo width as necessary.根据需要调整nav_logo width

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM