2

I have a table

\begin{tabularx}{\textwidth}{ X p{70pt}
\toprule
stuff1&stuff2\\ \midrule
stuff3&stuff4\\ \bottomrule
\end{tabularx}

and i would like to insert on its right a vector image with fixed width and adjust its height in such a way that the upper edge of the image is aligned to the \toprule of the table and the bottom edge of the image is aligned to the \bottomrule. I made many attempts but all of them failed.

EDIT:

Both of the solutions proposed give me layout problems on the pages following the one of the table, in particular the effect i observe is that the left margin is set to 0. Here below i put the full code hoping that you can find the cause. My aim is still the same the only thing changing is that i want the image on the left of the table.

\documentclass[a4paper,twoside]{article}
\usepackage{layout}
\usepackage{geometry}
\usepackage{fancyhdr}
\usepackage{layouts}
\usepackage{graphicx}
\usepackage[export]{adjustbox}
\usepackage{lastpage}
\usepackage[bottom]{footmisc}
\usepackage{eso-pic}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{tabularx}
\usepackage{array}
\usepackage{colortbl}
\usepackage{multirow}
\usepackage{multicol}
\usepackage{booktabs}
\usepackage[english]{babel}
\usepackage{float}
\usepackage{comment}
\usepackage{tikz}
\usepackage{ctable}
\usepackage{wrapfig}
\usepackage{makecell}
\usepackage{picture}
\usepackage{zref-savepos}






\definecolor{my_red}{RGB}{213,3,14}


%section
\titleformat{\section}
{\color{my_red}\normalfont\Large\bfseries}
{\color{my_red}\thesection}{1em}{}
%subsection
\titleformat{\subsection}
{\color{my_red}\normalfont\large\bfseries}
{\color{my_red}\thesubsection}{1em}{}
%subsubsection
\titleformat{\subsubsection}
{\color{my_red}\normalfont\normalsize\bfseries}
{\color{my_red}\thesubsubsection}{1em}{}



%Margins
\setlength{\hoffset}{-1in}
\setlength{\oddsidemargin}{1.5cm}
\setlength{\evensidemargin}{0cm}
\setlength{\marginparpush}{0pt}
\setlength{\marginparsep}{0pt}
\setlength{\marginparwidth}{0pt}
\setlength{\textwidth}{18cm}
\setlength{\parindent}{0pt}
\setlength{\voffset}{1.46cm}
\setlength{\topmargin}{-1in}
\setlength{\headheight}{1in}
\setlength{\headsep}{30pt}
\setlength{\textheight}{21.78696cm}


\newsavebox\mysavebox

\begin{document}

%
%FIRST PAGE
%
\pagestyle{fancy}
\fancyhead{} % clear all header fields
\fancyfoot[L]{stuff}
\fancyfoot[C]{}
\fancyfoot[R]{}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}


\begin{adjustbox}{gstore=\mysavebox}
\begin{tabularx}{.8\textwidth}{ X p{70pt}}
\toprule
stuff1&stuff2\\ \midrule
stuff3&stuff4\\ \bottomrule
\end{tabularx}%
\end{adjustbox}
\adjustimage{height=\ht\mysavebox+\dp\mysavebox, width=1cm, raise=-\dp\mysavebox}{margin.pdf}

\newpage    

%
%OTHER PAGES  
%


\pagestyle{fancy}
\fancyhead{} % clear all header fields
\fancyhead[R]{
        \begin{picture}(65,0)
        \includegraphics[ width=70pt,left]{Logo.pdf}
        \end{picture}
        }
\fancyhead[L]{
        {data}\vspace{40pt}
        }
\fancyhead[C]{
        title\vspace{40pt}
        }
\fancyfoot{} % clear all footer fields
\fancyfoot[LE]{stuff}
\fancyfoot[RE]{}
\fancyfoot[LO]{stuff \\ stuff}
\fancyfoot[RO]{\phantom{internal}\\ \thepage/\pageref{LastPage}}
\renewcommand{\headrulewidth}{1pt}
\renewcommand{\footrulewidth}{1pt}

\section{Section}
{alfhlkjhgldsfhgjh}\footnote{section}
textheight in cm: \printinunitsof{cm}\prntlen{\textheight}
\subsection{Subsection}
{alfhlkjhgldsfhgjh}\footnote{subsection}

\subsubsection{Subsubsection}
{alfhlkjhgldsfhgjh}\footnote{subsubsection}

\begin{itemize}
\item[{\color{my_red}$\bullet$} ]stuff
\item[{\color{my_red}$\bullet$} ]stuff
\end{itemize}

\begin{table}[H]
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt} 
\begin{tabularx}{\textwidth}{|X|X|}
\toprule
  \textbf{Symptom} & \textbf{Metric} \\ \midrule
Class that has many accessor methods and accesses a lot of external data & ATFD is more than a few\\ \midrule
Class that is large and complex & WMC is high\\ \midrule
Class that has a lot of methods that only operate on a proper subset of the instance variable set & TCC is low\\ \midrule
\end{tabularx}
\end{table}


\begin{table}[H]
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\begin{tabularx}{\textwidth}{|c|c|X|}
\toprule
\textbf{Version} &   \textbf{Date} & \textbf{Metric} \\
\midrule
01&28-01-2019& Class that has many accessor methods and accesses a lot of external data \\
\midrule
01&28-01-2019& Class that is large and complex \\
\midrule
01&28-01-2019& Class that has a lot of methods that only operate on a proper subset of the instance variable set \\
\midrule
\end{tabularx}
\end{table}

\begin{table}[H]
\setlength\aboverulesep{0pt}
\setlength\belowrulesep{0pt}
\begin{tabularx}{\textwidth}{|c|X|c|c|X|}
\toprule
\multicolumn{5}{|c|}{Stuff}\\
\cline{1-2}\cline{4-5}
28-01-2019& Class that has many accessor methods and accesses a lot of external data& \multirow{2}{*}{\phantom{a}}  &28-01-2019& Class that has many accessor methods and accesses a lot of external data \\ \cline{1-2}\cline{4-5}
28-01-2019& Class that has many accessor methods and accesses a lot of external data& & 28-01-2019& Class that has many accessor methods and accesses a lot of external data \\
\cline{1-2}\cline{4-5}
\end{tabularx}
\end{table}


\end{document}
  • Please always provide your code as a full, compilable example document. This makes it so much easier to code a solution. – Martin Scharrer Jan 29 at 12:24
2

You need to store the table in a box and use the box dimensions to scale the image. This can be achieved in a simple way with the adjustbox package.

\documentclass{article}

\usepackage{adjustbox}
\usepackage{tabularx}
\usepackage{booktabs}

\usepackage{mwe}% just for example code

\newsavebox\mysavebox
\begin{document}

\blindtext

\medskip

\begin{adjustbox}{gstore=\mysavebox}
\begin{tabularx}{.8\textwidth}{ X p{70pt}}
\toprule
stuff1&stuff2\\ \midrule
stuff3&stuff4\\ \bottomrule
\end{tabularx}%
\end{adjustbox}
\adjustimage{height=\ht\mysavebox+\dp\mysavebox,width=.2\textwidth,raise=-\dp\mysavebox}{example-image}

\medskip


\blindtext

\end{document}

enter image description here

If you want to draw the vector graphic using TeX code then change the \adjustimage to {adjustbox}. However, un that case you might want to use the box dimensions as TikZ settings instead to avoid distortions.

\documentclass{article}

\usepackage{adjustbox}
\usepackage{tabularx}
\usepackage{booktabs}
\usepackage{tikz}

\usepackage{mwe}% just for example code

\newsavebox\mysavebox
\begin{document}

\blindtext

\medskip

\begin{adjustbox}{gstore=\mysavebox}
\begin{tabularx}{.8\textwidth}{ X p{70pt}}
\toprule
stuff1&stuff2\\ \midrule
stuff3&stuff4\\ \bottomrule
\end{tabularx}%
\end{adjustbox}
\begin{adjustbox}{height=\ht\mysavebox+\dp\mysavebox,width=.2\textwidth,raise=\depth-\dp\mysavebox}
\begin{tikzpicture}
 \draw (0,0) -- (1,1);
 \draw (0,1) -- (1,0);
\end{tikzpicture}
\end{adjustbox}

\medskip


\blindtext

\end{document}

enter image description here


If you want to position the image on the left instead, you just need to change the gstore key to gstore* to make adjustbox not insert the saved box right away. Then place \usebox\mysavebox after the \includegraphics command. You need to watch hidden spaces here which might be added to the left of the image. Best is to end some lines with % to mask the line breaks which are turned into spaces. If you want a specific distance between the image and the table change the space with \hspace{<length>}.

\documentclass{article}

\usepackage{adjustbox}
\usepackage{tabularx}
\usepackage{booktabs}

\usepackage{mwe}% just for example code

\newsavebox\mysavebox
\begin{document}

\blindtext

\medskip

\begin{adjustbox}{gstore*=\mysavebox}
\begin{tabularx}{.8\textwidth}{ X p{70pt}}
\toprule
stuff1&stuff2\\ \midrule
stuff3&stuff4\\ \bottomrule
\end{tabularx}%
\end{adjustbox}%
\adjustimage{height=\ht\mysavebox+\dp\mysavebox,width=.19\textwidth,raise=-\dp\mysavebox}{example-image} \usebox\mysavebox

\medskip


\blindtext

\end{document}
  • Thanks a lot! how can i put it on the left insteoad of the right? – student Jan 29 at 14:58
  • @student: I updated my answer to show that. – Martin Scharrer Jan 30 at 6:56
  • Thank you very much @Martin the effect is indeed the one i need. I still have layout problems and i can't understand why, i reported the full code in the EDIT. If i comment the part with with your suggestion the layout of following pages is ok, if i uncomment it the following pages have distorted layout, can you help me pls? – student Jan 30 at 8:38
  • found the error, my bad! it was in the margin settings. Sorry! – student Jan 30 at 9:27
3

You can use zref to store the positions and use them to calculate the height:

\documentclass[]{article}
\usepackage{tabularx,graphicx,zref-savepos,booktabs}
\begin{document}

\begin{tabularx}{\textwidth}[b]{Xp{70pt}}
\noalign{\zsaveposy{top}}\toprule
stuff1&stuff2\\ \midrule
stuff3&stuff4\\ \bottomrule\noalign{\zsaveposy{bottom}}
\end{tabularx}%
\makebox[0pt][l]{\raisebox{0pt}[0pt][0pt]{%
\includegraphics[height=
\dimexpr\zposy{top}sp-\zposy{bottom}sp]
{example-image-duck}}}

\end{document}
  • Many thanks, this is exactly what i need! Unfortunately i'm experiencing a side effect of the solution you proposed. Indeed it modifies the layout of the following pages. How can i overcome this effect? – student Jan 29 at 14:46
  • There is nothing in the code that modifies the layout. – Ulrike Fischer Jan 29 at 14:48
  • i really don't know why this happens – student Jan 29 at 14:52

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.