이미지를 줄이지만 라텍스에서는 위로 올라가지 않습니다

StackOverflow https://stackoverflow.com/questions/122348

  •  02-07-2019
  •  | 
  •  

문제

포함 Graphics 명령이 포함 된 명령이 있습니다. 이미지를 내 명령에 전달할 수 있으며 실제로 이미지를 포함하기 전에 표준 형식을 수행합니다. 이 명령을 통해 내가 포함하는 일부 이미지는 textWidth보다 작고 일부는 더 크다. 더 큰 이미지를 textwidth로 스케일링하고 싶습니다. 작은 이미지를 확장하지는 않지만 이는 할 수 없다는 것을 의미합니다.

\includegraphics[width=\textwidth]{img}

MaxWidth를 지정하는 방법이 있습니까? 또는 어떻게 든 이미지의 너비를 얻을 수 있도록

\ifthenelse{\imagewidth > \textwidth}{%
    \includegraphics[width=\textwidth]{img}}{%
    \includegraphics{img}}
도움이 되었습니까?

해결책

이미지의 너비를 얻으려면이 코드를 사용할 수 있습니다.

\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}

이게 도움이 되길 바란다!

다른 팁

선택적으로 이미지를 선택적으로 스케일링하거나 약간 높이는 추가 매개 변수를 좋아하므로 scalegraphics 버전은 다음과 같습니다.

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

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.

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}}

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top