缩小图像,但在乳胶中不放大

发布于 2024-07-05 03:55:17 字数 433 浏览 7 评论 0原文

我有一个包含 includegraphics 命令的命令 - 我可以将图像传递给我的命令,它会在实际包含图像之前为我执行一些标准格式设置。 我通过此命令包含的一些图像小于 \textwidth,而另一些则更大。 我想将较大的图像缩小到 \textwidth,而不放大较小的图像 - 这意味着我不能只是这样做

\includegraphics[width=\textwidth]{img}

有没有办法指定 maxwidth? 或者,我可以以某种方式获取图像的宽度,这样我就可以做类似的事情

\ifthenelse{\imagewidth > \textwidth}{%
    \includegraphics[width=\textwidth]{img}}{%
    \includegraphics{img}}

I have a command which includes an includegraphics command - I can pass an image to my command, and it will do some standard formatting for me before actually including the image. Some of the images that I'm including via this command are smaller than \textwidth, while some are larger. I'd like to scale the larger images down to \textwidth, while not scaling the smaller images up - this means I can't just do

\includegraphics[width=\textwidth]{img}

Is there a way to specify a maxwidth? Or, can I get the width of the image somehow so I can do something like

\ifthenelse{\imagewidth > \textwidth}{%
    \includegraphics[width=\textwidth]{img}}{%
    \includegraphics{img}}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

携君以终年 2024-07-12 03:55:17

我喜欢一个附加参数来选择性地缩小或放大图像,所以我的 \scalegraphics 版本如下所示:

\newcommand\scalegraphics[2][]{%
    \settowidth{\imgwidth}{\includegraphics{#2}}%
    \setlength{\imgwidth}{\minof{#1\imgwidth}{\textwidth}}%
    \includegraphics[width=\imgwidth]{#2}%
}

I like an additional parameter for optionally scaling the image down or up a bit, so my version of \scalegraphics looks like this:

\newcommand\scalegraphics[2][]{%
    \settowidth{\imgwidth}{\includegraphics{#2}}%
    \setlength{\imgwidth}{\minof{#1\imgwidth}{\textwidth}}%
    \includegraphics[width=\imgwidth]{#2}%
}
微凉 2024-07-12 03:55:17

要获取图像的宽度,您可以使用以下代码:

\newlength{\imgwidth}
\settowidth{\imgwidth}{\includegraphics{img}}

您可以在文档序言中使用此代码来创建一个新命令来自动设置宽度:

\usepackage{graphicx}
\usepackage{calc}

\newlength{\imgwidth}

\newcommand\scalegraphics[1]{%   
    \settowidth{\imgwidth}{\includegraphics{#1}}%
    \setlength{\imgwidth}{\minof{\imgwidth}{\textwidth}}%
    \includegraphics[width=\imgwidth]{#1}%
}

然后,在您的文档中:

\scalegraphics{img}

我希望这会有所帮助!

To get the width of the image you can use this code:

\newlength{\imgwidth}
\settowidth{\imgwidth}{\includegraphics{img}}

You could use this in the document preamble to create a new command to automatically set the width:

\usepackage{graphicx}
\usepackage{calc}

\newlength{\imgwidth}

\newcommand\scalegraphics[1]{%   
    \settowidth{\imgwidth}{\includegraphics{#1}}%
    \setlength{\imgwidth}{\minof{\imgwidth}{\textwidth}}%
    \includegraphics[width=\imgwidth]{#1}%
}

and then, in your document:

\scalegraphics{img}

I hope this helps!

请爱~陌生人 2024-07-12 03:55:17

adjustbox 包对于这。 下面您将找到一个简短的示例。 它允许执行以下操作(除了修剪、剪裁、添加边距和相对缩放之外):

\documentclass[a4paper]{article}


\usepackage[demo]{graphicx}
\usepackage[export]{adjustbox}

\begin{document}

\adjustbox{max width=\linewidth}{\includegraphics[width=.5\linewidth,height=3cm]{}}

\adjustbox{max width=\linewidth}{\includegraphics[width=2\linewidth,height=3cm]{}}

\includegraphics[width=2\linewidth,height=3cm,max width=\linewidth]{}
\end{document}

如果您使用 export 包选项,则其大部分键都可以直接与 \includegraphics 一起使用。例如与您相关的键,最大宽度

The adjustbox package is usefull for this. Below you will find a short example. It allows the following (besides triming, clipping, adding margins and relative scaling:

\documentclass[a4paper]{article}


\usepackage[demo]{graphicx}
\usepackage[export]{adjustbox}

\begin{document}

\adjustbox{max width=\linewidth}{\includegraphics[width=.5\linewidth,height=3cm]{}}

\adjustbox{max width=\linewidth}{\includegraphics[width=2\linewidth,height=3cm]{}}

\includegraphics[width=2\linewidth,height=3cm,max width=\linewidth]{}
\end{document}

If you use the export package option most of its keys can be used directly with \includegraphics. FOr instance the key relevant to you, max width.

酒废 2024-07-12 03:55:17

如果你想要约束的不是图像而是一个独立的 .tex 文件,你可以稍微修改 ChrisN 的 \scalegraphics

\newlength{\inputwidth}
\newcommand\maxwidthinput[2][\linewidth]{%   
    \settowidth{\inputwidth}{#2}%
    \setlength{\inputwidth}{\minof{\inputwidth}{#1}}%
    \resizebox{\inputwidth}{!}{#2}
}

然后像这样使用它

\maxwidthinput{\input{standalone}}
\maxwidthinput[0.5\textwidth]{\input{standalone}}

当然, < code>adjustbox 按照 ted 的建议也可以工作:

\usepackage{adjustbox}
...
\adjustbox{max width=\linewidth}{\input{standalone}}

If what you want to constrain is not an image but a standalone .tex file, you can slightly modify ChrisN's \scalegraphics to

\newlength{\inputwidth}
\newcommand\maxwidthinput[2][\linewidth]{%   
    \settowidth{\inputwidth}{#2}%
    \setlength{\inputwidth}{\minof{\inputwidth}{#1}}%
    \resizebox{\inputwidth}{!}{#2}
}

and then use it like so

\maxwidthinput{\input{standalone}}
\maxwidthinput[0.5\textwidth]{\input{standalone}}

And of course, adjustbox as suggested by ted will work as well:

\usepackage{adjustbox}
...
\adjustbox{max width=\linewidth}{\input{standalone}}
木槿暧夏七纪年 2024-07-12 03:55:17

经过几分钟的 CTAN 手册和 Google 结果搜索后,我想我可以有把握地说,你想做的事情要么是不可能的,要么是非常困难的。 我唯一的建议是您有两个命令,一个用于小图像,一个用于大图像,或者一个带有选项的命令。

可能有办法,但我将其留给其他 SO LaTeX 向导来提供更好的答案。

编辑:我错了,见上文。

After a few minutes of searching through CTAN manuals and Google results, I think I can safely say that what you want to do is either impossible or very hard. My only recommendation is that you have two commands, one for small images and one for large, or one command with an option.

There may be a way, but I leave it to other S.O. LaTeX wizards to provide a better answer.

Edit: I am wrong, see above.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文