3

I am trying to create a special box for tables/pictures/text that spans the while width of the page, where the page's geometry is set to provide a margin (left/right alternating with odd/even). I would like to be able to customize the looks of the box frame and fill, so I opted for the tikz package because it lets me draw quite anything so far. To get an overall idea of what I am trying to accomplish have a look at POB1. There 'BOX 3.2' is such an framed environment that has many complex children like figures and text etc.

The many stack-exchange posts regarding similar questions could not provide a working solution to my problem (or I am just to stupid to integrate it).

For starters, I would love the 'figure' in my MWE (see below) to occupy the full width (minus the 1.5cm thin margin) effectively ignoring the margin defaults of 5cm set with 'geometry'. Also, this should work anywhere (even and odd pages).

\documentclass[twoside,openany]{book} 

\usepackage[paper=a4paper,margin=1in]{geometry}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{kantlipsum}

\definecolor{LightOcean}{RGB}{81, 147, 229 }
\definecolor{DeepOcean}{RGB}{51, 131, 229}

\begin{document}

\newgeometry{top=3cm,bottom=3cm,left=1.5cm,right=5cm,headsep=10pt}
\lipsum[1-6]

\begin{figure}
  \begin{tikzpicture}
    \draw (0,0) node[fill=LightOcean!10, text width=\textwidth, text justified, rectangle, draw=DeepOcean, thick, rounded corners=8pt, minimum height=6em]
      {\kant[2]};
  \end{tikzpicture}
  \caption{\kant[1]}
\end{figure}

\lipsum[3-10]

\end{document}

I was thinking of a minipage to encapsulate ... sub-minipages? But how would one tell tikzpicture where to draw the frames?

Many thanks in advance.

Edward
  • 75
  • 5
  • @marmot That isn't a good way to do it. changepage is the package wanted here. – cfr Nov 29 '17 at 03:53
  • @cfr '@marmot' ??? (confused)... Anyways, thank you for the hint 'changepage'. I looked it up and found https://tex.stackexchange.com/questions/6402/how-to-switch-between-two-different-marginparwidth-size where someone was trying to get exactly what I need. Following the MWE there one is able to get this done but the approach does not work on a page-break on two-sided documents. I.e. if the environment started on an even page and is broken halfway, then the other half will be start indented to the odd-page margin-notes. Not cool! If only someone had a solution to that problem as well... – Edward Nov 29 '17 at 06:05
  • There was a comment which has since been deleted. The first part of my comment was a response to the suggestion in that comment. I thought changepage had code for odd/even. However, I have not used it terribly often, so I'm not sure about this. But you are using a figure so page breaks aren't relevant here anyway. – cfr Nov 29 '17 at 18:26
  • @cfr changepage does have code for odd/even but remember that TeX sets paragraph by paragraph. If a pagebreak comes in the middle of a paragraph then the settings for that paragraph on the two pages will be the same. – Peter Wilson Nov 29 '17 at 18:51
  • @PeterWilson But LaTeX must break the setting in order to break the paragraph at all. In any case, a float, a tikzpicture, a box doesn't allow a page break .... – cfr Nov 30 '17 at 04:39
  • I found a possible solution browsing SE posts on 'mdframed' and 'vbox'. It does the job (mostly) with sometimes screwing with the margin space (as usual) if the placement would be at the page-break. I am tempted to opt for the solution provided by Ignasi as it seems to work better. – Edward Nov 30 '17 at 05:55
  • @cfr I think we might be talking about different things. The comment section is too small for my example about pagebreaking in the middle of a paragraph. I assume you are a TUG subscriber so please look at TUGboat, v38, no. 3, 2017 page 340 in the section titled "All is not what it seems". (Depending on your version of the given example it might need a \strictpagecheck before the adjustwidth* environment. – Peter Wilson Dec 07 '17 at 19:53

1 Answers1

3

I think tcolorbox is a better solution than TikZ to draw framed (floating or not) boxes:

\documentclass[twoside,openany]{book} 

\usepackage[a4paper, vmargin={3cm, 3cm}, hmargin={1.5cm, 5cm}, headsep=10pt]{geometry}
\usepackage{tikz}
\usepackage{lipsum}
\usepackage{kantlipsum}
\usepackage[most]{tcolorbox}
\usepackage{multicol}

\definecolor{LightOcean}{RGB}{81, 147, 229 }
\definecolor{DeepOcean}{RGB}{51, 131, 229}

\newtcolorbox{mybox}[1][]{%
    float, 
    floatplacement=t,
    enhanced, 
    colback=LightOcean!10, 
    colframe=DeepOcean,
%   show bounding box,
    notitle,
    grow to right by=\marginparsep+\marginparwidth-15mm,
    toggle enlargement=evenpage,
    #1
}

\begin{document}


\lipsum[1-3]

\begin{mybox}
\begin{multicols}{2}
\kant[1]
\end{multicols}

\begin{minipage}[c]{.4\linewidth}
\centering
\includegraphics[width=.8\linewidth]{example-image-a}
\end{minipage}\hfill
\begin{minipage}[c]{.55\linewidth}
\kant[1]
\end{minipage}
\end{mybox}

\lipsum[3-6]

\begin{mybox}
\kant[2]
\end{mybox}

\end{document}

enter image description here

Ignasi
  • 136,588
  • I have tested your suggested solution and it works quite well indeed. The tcolorbox behaves nicely like a float as was required, no breaks. A second compile pass resolves sometimes occurring margin-space erroneous placement. No problems here. However, I am having trouble disabling the middle separator line. In fact, I only need the title-bar and the outer frame customizable. I managed to set the titlebar's BG-col set to DeppOcean!50, colback to white, and colframe to DeepOcean!50... but following the manual I was not able to disable the lower frame-line just below the title-bar. Any hints? – Edward Nov 30 '17 at 05:58
  • @Edward I'm not sure to understand your problem without a code example. But if you are trying to include a title without visible separation between title and text, try with detach title, before upper={\tcbtitle\\}, but being sure that coltitle is different from colback. If you have a different problem, please open a new question with the corresponding code. – Ignasi Nov 30 '17 at 08:23
  • 'detach title, before upper={\tcbtitle\}' removed the horizontal line in the titlebar, but also the titlebar background fill, which was not what I wanted. But you are right, I will open a new question with a MWE for this. I will accept your answer because the solution is solid and does what was asked. – Edward Nov 30 '17 at 09:30
  • I was able to find the missing clue, thanks to the giant manual. Just need to add 'titlerule=0mm' and the horizontal line disappears. (Of course, 'notitle' must be removed from the above MWE to make the titlebar appear for starters.) – Edward Nov 30 '17 at 10:08