2

Continuing my previous question: Is there a way to use \scalebox in a way that the logo scales to a specific height? I want to put a logo I created in Tikz into a rectangle that I also created in Tikz. Both should be used as a chapter header. I used the \savebox command to save the logo beforehand. But now I want the logo to have the same height as the rectangle and both should have the same origin. Is it possible to use an expression like height of the rectangle / \ht\logo as the scaling factor somehow? This would be helpful should the logo ever change. Here is the code; the line that I think needs to be changed is marked.

\documentclass[a4paper,11pt,fleqn]{book}
\usepackage{xcolor}
\usepackage{color}
\definecolor{mydarkgray}{RGB}{0,100,100}
\newcommand{\mytemplatecolor}{mydarkgray}

\usepackage{tikz}

\newsavebox{\logo}
\savebox{\logo}{%
\begin{tikzpicture}[y=0.80pt, x=0.80pt, yscale=-1.000000, xscale=1.000000, inner sep=0pt, outer sep=0pt]
\path[draw=black,fill=black,line join=miter,line cap=round,miter
limit=4.00,nonzero rule,dash phase=3.000pt,line width=0.000pt]
(178.4915,651.0411) .. controls (178.0358,651.0411) and (177.5801,651.0411)..
 (177.1244,651.0411) .. controls (177.1244,652.6787) and (177.1244,654.3163) ..
 (177.1244,655.9539) .. controls (177.5801,655.9539) and (178.0358,655.9539) ..
 (178.4915,655.9539) .. controls (178.4915,654.3163) and (178.4915,652.6787) ..
 (178.4915,651.0411) -- cycle;
 \path[xscale=-1.000,yscale=1.000,draw=black,fill=black,line join=miter,line
 cap=round,miter limit=4.00,nonzero rule,dash phase=3.000pt,line
width=0.000pt,rounded corners=0.0000cm] (-182.0794,658.5070) rectangle
(-177.1244,660.6401);
\path[xscale=-1.000,yscale=1.000,draw=black,fill=black,line join=miter,line
cap=round,nonzero rule,dash phase=3.000pt,line width=0.000pt,rounded
corners=0.0000cm] (-280.8165,679.2454) rectangle (-193.6939,684.6964);
\path[xscale=-1.000,yscale=1.000,fill=black,nonzero rule,rounded
corners=0.0000cm] (-280.8165,694.2955) rectangle (-177.1245,696.3244);
\path[xscale=-1.000,yscale=1.000,draw=black,fill=black,line join=miter,line
cap=round,miter limit=4.00,nonzero rule,dash phase=3.000pt,line
width=0.000pt,rounded corners=0.0000cm] (-280.8165,716.4833) rectangle
(-177.1245,721.9073);
\path[xscale=-1.000,yscale=1.000,draw=black,fill=black,line join=miter,line
cap=round,nonzero rule,dash phase=3.000pt,line width=0.000pt,rounded
corners=0.0000cm] (-185.6345,669.1724) rectangle (-177.1244,673.9126);
\end{tikzpicture}}

\usepackage[explicit]{titlesec}
 \newcommand*\chapterlabel{}
\titleformat{\chapter}[display]  
{\Huge\bfseries\sffamily\color{\mytemplatecolor}}
{\gdef\chapterlabel{\thechapter\ }}    
{0pt} % separation between label and chapter-title
{\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-8cm,xshift=0cm] at (current page.north west)
{\begin{tikzpicture}[remember picture, overlay]
    \draw[name path=mainbox,fill=\mytemplatecolor,\mytemplatecolor] (0,0)        rectangle(35.5mm,15mm);% mainbox
    \path (0,0) ++(0.379\wd\logo,+0.3755\ht\logo) node {\scalebox{0.76}  {\usebox{\logo}}};% this works if i would finetune the settings. But i want the Logo to always appear with the exakt height of the mainbox and flush with the page boarder. No tiny overlap anywere as this looks bad when zooming inside the PDF
\node[anchor=north east,yshift=-7.2cm,xshift=34mm,minimum height=30mm,inner sep=0mm] at (current page.north west)
{\parbox[top][30mm][t]{15mm}{\raggedleft $\phantom{\textrm{l}}$\color{white}\chapterlabel}};  
\node[anchor=north west,yshift=-7.2cm,xshift=37mm,text     width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north west)
    {\parbox[top][30mm][t]{\textwidth}{\color{\mytemplatecolor}#1}};
    \end{tikzpicture}
};
\gdef\chapterlabel{} % code before the title body
\end{tikzpicture}
}
\titlespacing*{\chapter}{-98pt}{38pt}{30pt} % [A]


\begin{document}
\chapter{A Chapter} 

\end{document}  

If not can I somehow output the size of the logo to calculate manually the exact ratio? Can I specify a specific height in mm with \scalebox?

  • 2
    Welcome to TeX.SX! You can use \resizebox, rather than \scalebox. – egreg Jan 31 '17 at 11:28
  • Not that I understand what you're about, but I guess that \resizebox{0.379\width}{0.3755\height}{\usebox{logo}} is what you're looking for. – egreg Jan 31 '17 at 12:15
  • almost... what i wanted was \path (1.15,0.75) node {\resizebox{23mm}{15.15mm}{\usebox{\logo}}}; so the logo is 15 mm in height... the extra 0.15 mm are likely due to the fact that the visible part of the logo is not 15 mm. I am still stuck to position the logo anchored at the lower left corner of the outer Box. If i change the width of the logo i have to manually reposition it...this is a it annoying – NorrinRadd Jan 31 '17 at 13:03
  • For that you can use the node anchor. Instead of \path (1.15,0.75) node {\resizebox{23mm}{15.15mm}{\usebox{\logo}}}; use \node[above right=0mm, inner sep=0pt] at (0,0) {\resizebox{!}{15.15mm}{usebox{\logo}}};. The ! in the resizebox means you only want to scale the height and Keep the aspect Ratio of the Image (assuming that's what you want). – Guilherme Zanotelli Jan 31 '17 at 13:07
6

As egreg suggests, you can use \resizebox, it works like \scalebox, only instead of a scale factor you use a scale dimension:

\resizebox{<width dim>}{<height dim>}{contents to be scaled}

If you want to scale to an specific width/height whilst maintaining the aspect ratio. For that you give ! to the dependent dimension, e.g. in your case:

\resizebox{!}{<rectangle height>}{\usebox{\logo}}

So just replace the \scalebox part for \resizebox{!}{15mm}{\usebox{\logo}} and your scaling Problems are gone.

Now, there are some issues with positioning and some problems with the drawing itself. First thin is the background rectangle, it uses \draw with the fill option, so when positioning the logo we have to deal with \pgflinewidth, but since the line and the filling are of the same color, there's no need for \draw, use \path instead and the line won't be drawn anymore.

Now, to the node containing the logo. It is positioned by it's center so whenever the logo changes size you need to reposition it accordingly. Use the anchor=south west (equivalent to option above right) and place the node at (0,0) now the 'origin' of the node is at (0,0). But the node, by default, has some inner sep, we must set it to 0: inner sep=0pt. Furthermore, the node has an underlying box around it of width \pgflinewidth, which leaves a gap between the logo and the background box. You can either shift={(45:-\pgflinewidth)} or set line width=0pt (this is smarter) to remove this little space. So, after doing all that the drawing should be fine.

600% Zoom

MWE

\documentclass[a4paper,11pt,fleqn]{book}
\usepackage{xcolor}
\usepackage{color}
\definecolor{mydarkgray}{RGB}{0,100,100}
\newcommand{\mytemplatecolor}{mydarkgray}

\usepackage{tikz}
\usetikzlibrary{intersections}

\newsavebox{\logo}
\savebox{\logo}{%
\begin{tikzpicture}[y=0.80pt, x=0.80pt, yscale=-1.000000, xscale=1.000000, inner sep=0pt, outer sep=0pt]
\path[draw=black,fill=black,line join=miter,line cap=round,miter
limit=4.00,nonzero rule,dash phase=3.000pt,line width=0.000pt]
(178.4915,651.0411) .. controls (178.0358,651.0411) and (177.5801,651.0411)..
 (177.1244,651.0411) .. controls (177.1244,652.6787) and (177.1244,654.3163) ..
 (177.1244,655.9539) .. controls (177.5801,655.9539) and (178.0358,655.9539) ..
 (178.4915,655.9539) .. controls (178.4915,654.3163) and (178.4915,652.6787) ..
 (178.4915,651.0411) -- cycle;
 \path[xscale=-1.000,yscale=1.000,draw=black,fill=black,line join=miter,line
 cap=round,miter limit=4.00,nonzero rule,dash phase=3.000pt,line
width=0.000pt,rounded corners=0.0000cm] (-182.0794,658.5070) rectangle
(-177.1244,660.6401);
\path[xscale=-1.000,yscale=1.000,draw=black,fill=black,line join=miter,line
cap=round,nonzero rule,dash phase=3.000pt,line width=0.000pt,rounded
corners=0.0000cm] (-280.8165,679.2454) rectangle (-193.6939,684.6964);
\path[xscale=-1.000,yscale=1.000,fill=black,nonzero rule,rounded
corners=0.0000cm] (-280.8165,694.2955) rectangle (-177.1245,696.3244);
\path[xscale=-1.000,yscale=1.000,draw=black,fill=black,line join=miter,line
cap=round,miter limit=4.00,nonzero rule,dash phase=3.000pt,line
width=0.000pt,rounded corners=0.0000cm] (-280.8165,716.4833) rectangle
(-177.1245,721.9073);
\path[xscale=-1.000,yscale=1.000,draw=black,fill=black,line join=miter,line
cap=round,nonzero rule,dash phase=3.000pt,line width=0.000pt,rounded
corners=0.0000cm] (-185.6345,669.1724) rectangle (-177.1244,673.9126);
\end{tikzpicture}}

\usepackage[explicit]{titlesec}
 \newcommand*\chapterlabel{}
\titleformat{\chapter}[display]  
{\Huge\bfseries\sffamily\color{\mytemplatecolor}}
{\gdef\chapterlabel{\thechapter\ }}    
{0pt} % separation between label and chapter-title
{\begin{tikzpicture}[remember picture,overlay]
\node[yshift=-8cm,xshift=0cm] at (current page.north west)
{\begin{tikzpicture}[remember picture, overlay]
    \path[name path=mainbox,fill=\mytemplatecolor,\mytemplatecolor] (0,0)        rectangle (35.5mm,15mm);% mainbox
    \node[line width=0pt, above right=0mm, draw=none, inner sep=0pt] at (0,0) {\resizebox{!}{15mm}{\usebox{\logo}}};
\node[anchor=north east,yshift=-7.2cm,xshift=34mm,minimum height=30mm,inner sep=0mm] at (current page.north west)
{\parbox[top][30mm][t]{15mm}{\raggedleft $\phantom{\textrm{l}}$\color{white}\chapterlabel}};  
\node[anchor=north west,yshift=-7.2cm,xshift=37mm,text     width=\textwidth,minimum height=30mm,inner sep=0mm] at (current page.north west)
    {\parbox[top][30mm][t]{\textwidth}{\color{\mytemplatecolor}#1}};
    \end{tikzpicture}
};
\gdef\chapterlabel{} % code before the title body
\end{tikzpicture}
}
\titlespacing*{\chapter}{-98pt}{38pt}{30pt} % [A]


\begin{document}
\chapter{A Chapter} 

\end{document}  
  • Thank you for your answer! Your solution seems to include more additional code wich i do not understand? If i compile just the code up until the first \end{document} i almost get the desired result, but when i zoom into the pdf the logo still seems to be slightly larger? Am i doing somehing wrong ? Again thank you very much for taking the time to write an answer! – NorrinRadd Jan 31 '17 at 14:32
  • I supposie i am doing something wrong !my result. – NorrinRadd Jan 31 '17 at 14:40
  • @NorrinRadd, your result is perfectly ok. The impression you're getting that the logo is bigger than the background rectangle is a viewer artifact (If you zoom in ridiculously you'll see that). Every bit of code I added is explained in the answer above, and I apologize for the MWE, indeed I copied more than I should have. – Guilherme Zanotelli Jan 31 '17 at 14:53

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.