I would use a slightly different approach, first try and get everything into a macro and enable usage of cm throughout. As a point of reference is best to use the bottom left hand corner of the box like a normal plot (easier to remember and to visualize).
Here is the MWE:
\documentclass{article}
\usepackage{graphicx,environ}
\usepackage{overpic}
\usepackage{picture}
\usepackage{xcolor}
\def\sampletext{Some text that will fit
Some text that will fit
Some text that will fit
Some text that will fit.}
\fboxsep=0pt
\fboxrule=0pt
\begin{document}
% #1 x position
% #2 y position
% #3 width
% #4 height
% #5 graphics
\long\def\combo(#1,#2)#3#4#5#6{%
\begin{overpic}[width=#3, height=#4, grid,tics=10]{#5}
\parindent0pt
\put(#1,#2){%
\vbox{\leftskip5pt\rightskip5pt\hsize #3\color{white}\sffamily
#6\par}%
}%
\end{overpic}}
\combo(0cm,1cm){10cm}{8cm}{./graphics/cardtrick}{\large \sampletext}
\medskip
\combo(0cm,1cm){10cm}{8cm}{./graphics/cardtrick}{\Large\bfseries \sampletext}
\end{document}
Ideally, this should be a key value macro. If I understood your requirements correctly please drop a comment and I will extend it to a key value answer. Please also note that I have added a \leftskip
and a \rightskip
to allow for a little bit of padding. You can zero it if you don't need it.
UPDATED VERSION
Version uses key values for exact position, padding borders and fitting text into a set of given parameters by re-sizing fonts.
Text can be aligned using the key values justified
, raggedleft
, raggedright
and from the ragged2e
package RaggedLeft
and RaggedRight
.
Typical entry with a few adjustments,
\combo[x=0pt,y=0pt,
padding-left=5pt,
padding-right=5pt,
text-color=yellow,
text-width=5cm,
text-height=5cm,
image-width=13cm,
image-height=9.5cm,
image-url=cardtrick,
tics, showframe=true,
]{\lipsum[2]}
Below is a MWE with the code, a bit longish but I am hoping to polish it a bit and create a proper |.dtx| file and package which I will post at github soon.
\documentclass{article}
\usepackage[latin]{babel}
\usepackage{graphicx,lipsum}
\usepackage{overpic}
\usepackage{picture}
\usepackage{xcolor}
\usepackage{ragged2e}
\usepackage{combo}
\RequirePackage{pgf}[2008/01/15]
\fboxsep=0pt
\fboxrule=1pt
\makeatletter
% We use two boxes, one to store the contents of the text
% and another to store and measure the image
\newbox\textbox
% newif
\newif\if@ok
\@oktrue
\newif\ifshowframe
\newif\if@center
\newif\if@vcenter
% Step one define family
\pgfkeys{/combo/.is family}
% We store keys mostly in their own macros
\pgfkeys{/combo
% text positioning, reference is 0,0 at the left hand
% corner of the image
x/.store in=\position@x,
y/.store in=\position@y,
% text box padding
padding-left/.store in=\padding@left,
padding-left/.default=0pt,
padding-right/.store in=\padding@right,
padding-right/.default=0pt,
%
padding-bottom/.store in=\padding@bottom,
padding-bottom/.default=0pt,
%
padding-top/.store in=\padding@top,
padding-top/.default = 0pt,
% text coloring
text-color/.store in=\combo@textcolor,
text-color/.default=white,
% text width
text-width/.store in=\combo@textwidth,
text-width/.default=100pt,
text-height/.store in=\combo@textheight,
text-height/.default=5cm,
border-width/.store in=\combo@borderwidth,
border-color/.store in=\combo@bordercolor,
border-color/.default=red,
image-width/.store in=\combo@imgwidth,
image-width/.default=5cm,
image-height/.store in=\combo@imgheight,
image-height/.default=5cm,
image-url/.store in=\combo@imageurl,
% the file name for the graphic
image-url/.default=cardtrick,
tics/.store in=\combo@tics,
tics/.default=10,
showgrid/.store in=\combo@grid,
showgrid/.default ={,},
% define show frame key
showframe/.is if= showframe,
align/.is choice,
align/center/.code={\@centertrue},
align/vcenter/.code ={\@vcentertrue},
text-align/.store in = \combo@textalign,
border/.style={border-color=blue}
}
% Process keys and set defaults, for later use
\def\setdefaults{\pgfkeys{/combo
x=12pt,y=50pt,
padding-left,
padding-right,
padding-bottom,
text-align=raggedleft,
padding-top=0pt,
showgrid=grid,
}%
}
\setdefaults
% The approach we use is to have a set of predefined sizes
% to try out. If we cannot use any of these sizes, we fall back
% to scaling the fonts.
%
% We use a the list |\font@size@list| to hold all the allowable
% sizes for text. We also provide a command to add other sizes.
\newcommand{\font@size@list}{%
\Huge,\huge,
\LARGE,\Large
\large,\normalsize,
\small,\footnotesize,,
\scriptsize,\tiny%
}
% Author command holding default font size
% nothing else needed
\newcommand\default@fontsize{\small}
% Getter and setter for fontsize
% set@text@size
\def\set@text@size#1{%
\def\combo@text@size{#1}%
}
% we set default at \small
\set@text@size{\default@fontsize}
% holds the font details (Must add shape). Can be a bit automated
% as well
\newcommand\combo@inbox{}
% #2 x position of text block
% #3 y position
% #4 width
% #5 height
% #6 graphic
%
% We use pgf keys
\newcommand{\combo}[2][]{%
\setdefaults
\pgfkeys{/combo #1}
\def\combo@color{\color{\combo@textcolor}}
\def\combofont{%
\bfseries\rmfamily
\slshape\selectfont
\combo@text@size\combo@color
}%
% We store the contents of the text block in a
% box in order to measure the dimensions
\renewcommand{\combo@inbox}{%
\sbox\textbox{\par \vbox{%
\vskip\padding@top
\leftskip\padding@left%
\rightskip\padding@right%
\hsize \combo@textwidth%
\combofont%
\expandafter\csname\combo@textalign\endcsname
#2
\vskip\padding@bottom
\par
}%
}
}
\def\store@fontsize##1{%
\def\selected@fontsize{##1}}
% We iterate through all the size to get
% find an acceptable size that can fit in the box
\@for\next:=\font@size@list\do{%
\expandafter\set@text@size\next%
\next
% check and remeasure box
\combo@inbox
% \texttt{\expandafter\strip@prefix\meaning\next \the\ht\textbox}%
\if@ok
\ifdim\the\ht\textbox<\combo@textheight\relax
\@okfalse
%\fbox{\copy\textbox}\par%
\expandafter\store@fontsize\next
\fi
\fi
}
% if not found we have reached here with no solution
% better log an error and retry, by scaling the box
\if@ok\store@fontsize{\tiny} THIS IS IN ERROR \else \fi
\@oktrue
% We use the overpic package to set a backgroundgrid and
% to place the origin of the text box.
\vspace{1.5\baselineskip}
\centering
\begin{overpic}[width=\combo@imgwidth,
height=\combo@imgheight,
grid=\combo@grid,
tics=\combo@tics]{\combo@imageurl}%
\set@text@size\selected@fontsize
\combo@inbox
\put(\position@x,\the\dimexpr\position@y+\padding@bottom\relax){%
{\ifshowframe
\color{\combo@bordercolor}\fboxrule1pt\fbox{\copy\textbox}
\else
\color{red}\fboxrule0pt\fbox{\copy\textbox}
\fi
}%
}
\end{overpic}
}
\begin{document}
\combo[x=5pt,y=80pt,
padding-left=0pt,
padding-right=0pt,
padding-bottom=0pt,
text-color,
text-width=6cm,
text-height=5cm,
image-width=13cm,
image-height=7.5cm,
image-url=mandela,
tics,showframe=true,
border-color=yellow,
text-align=raggedright,
showframe=true,
showgrid=true,
border={border-color=blue}
]{Mandela has not made a public appearance since
the 2010 World Cup final in Johannesburg.}
\newpage
\combo[x=0pt,y=0pt,
padding-left=5pt,
padding-right=5pt,
text-color=yellow,
text-width=5cm,
text-height=5cm,
image-width=13cm,
image-height=9.5cm,
image-url=cardtrick,
tics, showframe=true,
]{\lipsum[2]}
\end{document}
picture
package, that extends thepicture
environment so that coordinates can be expressed also with the unit of measure or with parameters such as.3\textwidth
?picture
package. That was due to two reasons i) I didn't figure out how to run 'pdflatex -enable-write18' using TeXnicCentre and ii) I couldn't directly see how it should be used from its documentationpicture
requires-shell-escape
. Actually a small test I made seems to work.picture
package works fine. But it is still not obvious for me how to go from here...