以nbconvert为中心的图形标题? [英] figure caption centering in nbconvert?

查看:110
本文介绍了以nbconvert为中心的图形标题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ipython笔记本中图形下方的单元格中包含图形标题文本.我希望它们保持居中对齐.我在降价促销中使用了< center>",这恰好给出了我在笔记本中的外观.但是,当将nb转换为乳胶时,文本将被移至左侧.

I have cells underneath figures in an ipython notebook that contain figure caption text. I would like them to be centre('center')-aligned. I use "< center >" in the markdown, which gives exactly the appearance I'm after in the notebook. But when nb-converting to latex, the text gets shunted over to the left.

那么当转换为乳胶时,有没有办法让nbconvert识别降价单元中的文本对齐方式?

So is there a way to get nbconvert to recognize text alignment in markdown cells when converting to latex?

谢谢.

推荐答案

您实际上问了两个不同的问题:

You have actually asked two different questions:

  1. 有没有办法让nbconvert识别降价单元格中的文本对齐方式
  2. nbconvert中的图形标题(居中)

广告1)
要将降价转换为乳胶pandoc.不幸的是,如果将docdown转换为latex,则pandoc将从markdown中删除原始html(如果将markdown转换为html时,也将其删除rawlatex). 因此,使用html标签来格式化html和乳胶输出的格式并不是那么简单.这种格式化可以基于单元元数据来实现,但是目前还不算什么.

ad 1)
To convert the markdown to latex pandoc is used. Unfortunately, pandoc removes raw html from markdown if converted to latex (if also removes raw latex when converting markdown to html). So it is not that straight forward to use html tags to format the output in both html and latex. This formatting may be achieved based cell metadata but that is not that trivial currently.

广告2)
不过,可以创建类似于文本的标题以与html和乳胶一起使用. 在这里,我们必须区分pyout或流数据的标题(例如Ipython.display.Image)和降价图像.

ad 2)
Nevertheless it is possible to create caption like text to work with html and latex. Here we have to distinguish between caption for pyout or stream data (e.g. Ipython.display.Image) and markdown images.

一种可能的方法是创建一个 Caption 类,例如

A possible approach is to create a Caption class like

class Caption():
    def __init__(self,s):
        self.s = s
    def _repr_html_(self):
        return '<center>{0}</center>'.format(self.s)
    def _repr_latex_(self):
        return '\\begin{center}\n'+self.s+'\n\\end{center}'

在图像后被调用.请注意,两者均应使用IPython.display.display方法调用,例如,作为oneliner

which is called after the image. Note that both should be called with the IPython.display.display method, e.g as oneliner

display(Image('image.jpg'),Caption('Figure Caption'))

这种方法允许使用python处理字幕文本,例如添加数字编号.
如果要将这样的标题添加到matplotlib图中,由于必须克服错误的顺序,因此会有些棘手.一种可行的方法是使用此代码段进行绘制

This approach allows process the captiontext with python, e.g. to add figure numbers.
If you want to add such a caption to a matplotlib plot, it is a bit more tricky as the wrong ordering has to be overcome. A possible approach is to plot using this snippet

%matplotlib inline
plt.plot([1,2])
f=plt.gcf()
plt.close()
display(f,Caption('Plot'))

应该指出的是,IPython 1.x的默认乳胶模板不能很好地与这种方法配合使用,因为在这里,图像和标题仅是松散耦合的,因此在乳胶编译期间可能会包含垂直空间. latex_basic模板效果更好.在IPython master中,默认模板可以正常工作.

It may be noted the the default latex template of IPython 1.x doesn't play well with this approach, as here, image and caption are only loosely coupled and thus, vertical space might be included during latex compiling. The latex_basic template works much better. In IPython master the default templates are working fine.

Markdown允许使用

Markdown allows to use images like

![Caption](/files/image)

转换为乳胶时,pandoc可以使用标题"部分并创建真实的乳胶标题. 类似地,当转换为html时,字幕将嵌入在字幕类中,以便可以使用CSS轻松设置样式. 但是,当前IPython要求使用"/files/"前缀,该前缀目前尚未删除,因此乳胶无法找到该图像文件.(目前已修复)
请注意,这些markdown图片调用不会嵌入,而只是将图片链接到ipynb文件,因此,该图片必须保持可用状态.

When converting to latex pandoc can take the Caption part and create a real latex caption. Similar, when converting to html the caption gets embedded in a caption class to be easily styleable using css. However, currently IPython requires a "/files/" prefix which is currently not removed, thus the image file won't be found by latex. (Fixed by now)
Be aware that these markdown image calls do not embed but only link the image into the ipynb file, therefore, the image has to remain available.

这篇关于以nbconvert为中心的图形标题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆