0

If I do: \includegraphics[width=\textwidth-\leftskip]{image.png}

I don't get the same picture size as if I subtract its values of 483pt-60pt = 423pt.

But if i do: \includegraphics[width=\textwidth-10em]{image.png}

the image gets smaller...

How can i subtract \leftskip from \textwidth inside the width preamble of the graphic expression?

Here is my someway complicated MWE:

    \documentclass{article}
    \usepackage[
    left=20mm,
    right=20mm,
    top=20mm,
    bottom=18mm,
    footskip=2em,
    headheight=2em,
    headsep=0.8em
    ]{geometry}
    \usepackage[utf8]{inputenc}
    \usepackage{graphicx}   
    \usepackage{lipsum} 
    \leftskip=60pt                      
    \setlength{\parskip}{\baselineskip}      
    \setlength{\parindent}{0pt}             
      \setlength{\parskip}{0pt}
      \setlength{\textwidth}{483pt}         
    \usepackage{titlesec}
    \titleformat{\section}
       {\normalfont\fontsize{17pt}{18pt}\selectfont\scshape}{\thesection}{15mm}{}
    \titleformat{\subsection}
       {\normalfont\fontsize{16pt}{17pt}\selectfont\scshape}{\thesubsection}{10mm}{}
    \titleformat{\subsubsection}
       {\normalfont\fontsize{15pt}{16pt}\selectfont\scshape}{\thesubsubsection}{10mm}{}
    \titleformat{\paragraph}
       {\normalfont\fontsize{14pt}{15pt}\selectfont\scshape}{\theparagraph}{10mm}{} 
    \titleformat{\subparagraph}
       {\normalfont\fontsize{13pt}{14pt}\selectfont\scshape}{\thesubparagraph}{10mm}{}
    \begin{document}
    \section{one}
    \lipsum[1]
    \subsection{subone}
    \lipsum[2]
      \begin{figure}[!ht]
        \begin{flushright}
          \includegraphics[width=\textwidth]{example-image}
        \end{flushright}
      \end{figure}
    \section{two}
    \lipsum[3]
    \subsection{subtwo}
      \begin{figure}[!ht]
        \begin{flushright}
          \includegraphics[width=\dimexpr\textwidth-\leftskip\relax]{example-image}
        \end{flushright}
      \end{figure}
    \lipsum[4]
    \end{document} 

what im looking for

  • 1
    you should never directly access \leftskip in a latex document, are you looking for \linewidth ? however \dimexpr\textwidth-\leftskip\relax probably works – David Carlisle Nov 19 '17 at 18:25
  • I know my need is somekind special and not covered by article. Anyway: \dimexpr\textwidth-\leftskip\relax does not work. – novski Nov 19 '17 at 18:35
  • 1
    if they don't work then \leftskip presumably doesn't have the value that you expect at the point it is evaluated. It is hard to guess what is happening as you have shown no code. Every question really needs a complete small test file that allows the code to be debugged (you can use example-image as the image as that is available for such tests – David Carlisle Nov 19 '17 at 18:39
  • i added a MWE. Duno if its relevant that i use the \leftskip to get my textfields shifted right below the section number... – novski Nov 19 '17 at 19:15
  • as I say, setting \leftskip directly is always wrong, latex assumes that \@totalleftmargin, \lineskip, \textwidth and \hsize are in correct relationship and if you set one without resetting the others to match, things will break. – David Carlisle Nov 19 '17 at 19:37
3

The expression \dimexpr\textwidth-\leftskip works, however \leftskip inside flushright is 0.0pt plus 1.0fil so acts as 0pt in a \dimexpr so the value is \textwidth.

enter image description here

  \documentclass{article}
    \usepackage[
    left=20mm,
    right=20mm,
    top=20mm,
    bottom=18mm,
    footskip=2em,
    headheight=2em,
    headsep=0.8em
    ]{geometry}
    \usepackage[utf8]{inputenc}
    \usepackage{graphicx}   
    \usepackage{lipsum} 

    \setlength{\parskip}{\baselineskip} % this is not used     
    \setlength{\parindent}{0pt}             
      \setlength{\parskip}{0pt}% really?
      \setlength{\textwidth}{483pt}

    \addtolength\oddsidemargin{60pt}       
    \usepackage{titlesec}
    \titleformat{\section}
       {\normalfont\fontsize{17pt}{18pt}\selectfont\scshape}{\hspace*{-\oddsidemargin}\rlap{\thesection}}{\oddsidemargin}{}
    \titleformat{\subsection}
       {\normalfont\fontsize{16pt}{17pt}\selectfont\scshape}{\hspace*{-\oddsidemargin}\rlap{\thesubsection}}{\oddsidemargin}{}
    \titleformat{\subsubsection}
       {\normalfont\fontsize{15pt}{16pt}\selectfont\scshape}{\thesubsubsection}{10mm}{}
    \titleformat{\paragraph}
       {\normalfont\fontsize{14pt}{15pt}\selectfont\scshape}{\theparagraph}{10mm}{} 
    \titleformat{\subparagraph}
       {\normalfont\fontsize{13pt}{14pt}\selectfont\scshape}{\thesubparagraph}{10mm}{}
    \begin{document}
    \section{one}
    \lipsum[1]
    \subsection{subone}
    \lipsum[2]


          \includegraphics[width=\linewidth]{example-image}

    \section{two}
    \lipsum[3]
    \subsection{subtwo}
          \includegraphics[width=\linewidth]{example-image}

    \lipsum[4]
    \end{document} 
  • ok so im asking for the wrong thing. I am looking for the now added redline in the new image. How can i get the image only that wide? – novski Nov 19 '17 at 19:48
  • the length if the current textblock is \linewidth so you should be using that but also it should not be inside figure. The whole point of figure is that it is a float that will not appear in place inside the indented region so it normalises to document width and forgets the current indentation. If you use \includegraphics directly it would be positioned just as a letter is positioned , and fall naturally at the left margin. @novski – David Carlisle Nov 19 '17 at 20:00
  • @novski I added a suggested version (although really that is unrelated to the original question about arithmetic in width=) – David Carlisle Nov 19 '17 at 20:18
  • you probably want to reduce \textwidth by 60pt in my version to match the increas in oddsidemargin – David Carlisle Nov 19 '17 at 20:46
  • I copied the original code here but it doesn't make a lot of sense to load geometry to set the page size and margins in a consistent way, then change settings such as \textwidth "by hand" just set the page format you want using geometry – David Carlisle Nov 19 '17 at 21:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.