我刚开始接触Markdown。我喜欢它,但有一件事困扰着我:如何使用Markdown更改图像的大小?

文档仅为图像提供以下建议:

![drawing](drawing.jpg)

如果可能的话,我希望图片也居中。我问的是Markdown将军,而不仅仅是GitHub是如何做到的。


当前回答

对于那些热衷于手工和针织解决方案的人。有一些方法可以在不使用html的情况下调整.rmd文件中的图像大小:

您可以简单地通过添加{width=123px}来指定图像的宽度。不要在括号之间引入空格:

![image description]('your-image.png'){width=250px}

另一种选择是使用knifer::include_graphics:

```{r, fig.cap="image description", out.width = '50%'}
knitr::include_graphics('your-image.png')
```

其他回答

如果您正在使用kramdown,可以执行以下操作:

{:.foo}
![drawing](drawing.jpg)

然后将其添加到自定义CSS中:

.foo {
  text-align: center;
  width: 100px;
}

也许这一点最近有所改变,但Kramdown文档显示了一个简单的解决方案。

从文档中

Here is an inline ![smiley](smiley.png){:height="36px" width="36px"}.

And here is a referenced ![smile]

[smile]: smile.png
{: height="36px" width="36px"}

与Jekyll和Kramdown一起在github上工作。

通过纯向后兼容MD:

![<alt>](<imguri>#<w>x<h> "<title>")

其中w,h定义了要纵横适配的边界框,例如在Flutter包中https://pub.dev/packages/flutter_markdown

代码:https://github.com/flutter/packages/blob/9e8f5227ac14026c419f481ed1dfcb7b53961475/packages/flutter_markdown/lib/src/builder.dart#L473

重新考虑破坏兼容性的html解决方案,因为人们可能会使用本机/非html组件/应用程序来显示标记。

如果使用markdown,首先需要启用HTML渲染:

const md = require("markdown-it")({
  html: true,
});

然后可以在.md文件中使用:

<img src="" alt="drawing" width="100%" height="200"/>

如果每个md文件中有一个图像,控制图像大小的一种简便方法是:

添加css样式如下:

## Who Invented JSON?
`Douglas Crockford`

Douglas Crockford originally specified the JSON format in the early 2000s.
![Douglas Crockford](img/Douglas_Crockford.jpg)

<style type="text/css">
    img {
        width: 250px;
    }
</style>

输出如下:

如果每个md页面中有更多的图像,那么控制每个图像或每个自定义标记的简便方法是在css中定义每个元素。对于img标记,我们可以有:

//在css或样式标签中:img[alt=“结果1”]{宽度:100px;}img[alt=“结果2”]{宽度:200px;}img[alt=“结果3”]{宽度:400px;}//尝试使用以下方法之一在文档中插入图像:<br/><img src=“https://i.stack.imgur.com/xUb54.png“alt=”结果1“><br/><img src=“https://i.stack.imgur.com/xUb54.png“alt=”结果2“><br/><img src=“https://i.stack.imgur.com/xUb54.png“alt=”结果3“><br/><br/>在md中:<br/>![结果1](img/res-img-1.png)<br/>![结果2](img/res-img-2.png)<br/>![结果3](img/res-img-3.png)