[Tex/LaTex] Highlight parbox with frame

boxeshighlightingparboxspacing

I have a little problem.
I use parbox and textblock to position my text in front of a more or less complicated background-image.

Now I would like to highlight my parboxes to see if all margins are set correctly. However, I still want to see the background through, only have a red (potentially dashed) frame around and, most importantly, I want to be able to switch this frame off with a command in the preamble without any movement of my content.

I tried fbox and fcolorbox and was in both cases not even able to achieve non-moving content – not speaking of the other things I would like to achieve.

Any ideas how to implement something like that?

Thanks a lot in advance!

Edit:

Sorry about that. Here some example code

\documentclass[fontsize=10pt]{scrartcl}

\usepackage{lipsum}
\usepackage[overlay]{textpos}
\usepackage{color}

%% BACKGROUND  <<------ activate for actual background
%
%\usepackage{bophook}
%\usepackage{xcolor}
%\usepackage{tikz}
%
%\definecolor{dark}{RGB}{25,49,83}
%\colorlet{light}{dark!10}
%
%\newcommand{\topbar}{\draw[fill=dark, draw=none] (0,0) rectangle (216mm,-83mm);}
%\newcommand{\sidepanel}{\draw[fill=light, draw=none] (13mm,-23mm) rectangle (83mm,-280mm);}
%\newcommand{\footer}{%
%   \draw[fill=light, draw=none] (0mm,-303mm) rectangle (216mm,-290mm);%
%   \filldraw [fill=dark, draw=none] (184mm,-292.5mm)  coordinate (GeneralStart) -- ++(0,0) -- ++(+3.2,0) -- ++(0,-1.05) -- ++(-4.0,0) -- cycle;}
%
%\newcommand{\setbg}[1]{%
%   \AtBeginPage{%
%       \begin{tikzpicture}[remember picture,overlay]
%       \node[xshift=-108mm, yshift=151.5mm] at (current page.center)
%       {%
%           \begin{tikzpicture}[remember picture, overlay]%
%           #1
%           \end{tikzpicture}
%       };
%   \end{tikzpicture}
%}}


% HIGHLIGHTING


\newcommand{\lbox}[1]{\color{red}\fbox{ \normalcolor{#1}}}
%\renewcommand{\lbox}[1]{#1}  <<------- TOGGLE THIS


\begin{document}

%\setbg{\topbar\sidepanel\footer} <<------ activate for actual background

\begin{textblock*}{187mm}(-30mm,45mm)
    \lbox{%
        \parbox[c][187mm][c]{62mm}{%
            \lipsum[1-2]%
        }}
    \end{textblock*}

\end{document}

Toggle the marked content. As you will see, the text shifts, unfortunately. The only thing I actually want is to see on paper where my parbox is. The text shall not react to the box.

Best Answer

You can add a frame around the content using the adjustbox package. All you need is to remove the extra space by a matching negative margin. So you need to use \adjustbox{cfbox=red, margin=-\fboxrule-\fboxsep}{...}. Check the adjustbox manual for details if you want to know more.

\documentclass[fontsize=10pt]{scrartcl}
\usepackage{adjustbox}

\usepackage{lipsum}
\usepackage[overlay]{textpos}
\usepackage{color}

%% BACKGROUND  <<------ activate for actual background
\newif\ifbackground

\backgroundtrue

\ifbackground
\usepackage{bophook}
\usepackage{xcolor}
\usepackage{tikz}

\definecolor{dark}{RGB}{25,49,83}
\colorlet{light}{dark!10}

\newcommand{\topbar}{\draw[fill=dark, draw=none] (0,0) rectangle (216mm,-83mm);}
\newcommand{\sidepanel}{\draw[fill=light, draw=none] (13mm,-23mm) rectangle (83mm,-280mm);}
\newcommand{\footer}{%
   \draw[fill=light, draw=none] (0mm,-303mm) rectangle (216mm,-290mm);%
  \filldraw [fill=dark, draw=none] (184mm,-292.5mm)  coordinate (GeneralStart) -- ++(0,0) -- ++(+3.2,0) -- ++(0,-1.05) -- ++(-4.0,0) -- cycle;}

\newcommand{\setbg}[1]{%
   \AtBeginPage{%
      \begin{tikzpicture}[remember picture,overlay]
       \node[xshift=-108mm, yshift=151.5mm] at (current page.center)
       {%
           \begin{tikzpicture}[remember picture, overlay]%
          #1
           \end{tikzpicture}
       };
   \end{tikzpicture}
}}
\fi

% HIGHLIGHTING

\newcommand{\lbox}{\adjustbox{cfbox=red, margin=-\fboxrule-\fboxsep}}
%\renewcommand{\lbox}{\fbox}
%\renewcommand{\lbox}[1]{#1} % <<------- TOGGLE THIS


\begin{document}
\ifbackground
\setbg{\topbar\sidepanel\footer} %<<------ activate for actual background
\fi

\begin{textblock*}{187mm}(-30mm,45mm)
    \lbox{%
        \parbox[c][187mm][c]{62mm}{%
            \lipsum[1-2]%
        }}
    \end{textblock*}

\end{document}

PS: Also note the usage of an custom \if... switch. You should not use % to deactivate a large part of code.

Related Question