2

I have a color box (made with tcolorbox) that holds text, and I want to frame it. Unfortunately, I'm copying a style so I can't use a normal border. The border I want is only on the sides of the box with a dot at each end.

(Like this but black)

I've seen threads on here before that say you can overlay tikz drawings ontop of tcolorboxes, but that won't work. If what I want to accomplish isn't possible, I'll end up using that but I'd prefer to use an image. Since it's a class, I want it to be as lightweight as possible.

There are two problems. For one, I don't know how to position images overtop something like a tcolorbox. The second problem is that the height of these boxes is variable (based on the text). I don't want to stretch the border image because I want the dots to remain circle, but I want the actual border part to elongate.

So my actual question:

1) How do I position a graphic precisely following a border (corner as an anchor)?

2) How can I scale my image vertically without stretching the image?


Minimum working example:

\documentclass[10pt,oneside,twocolumn]{book}
\usepackage{tcolorbox}
\usepackage{xcolor}
\usepackage{lipsum}

\definecolor{textbox}{HTML}{D5D2DD}

\newtcolorbox{textbox}{
  colframe=textbox,
  colback=textbox,
  top=1mm, bottom=1mm, left=1.5mm, right=1.5mm,
  arc=0mm,
}

\begin{document}
  \raggedright

  \lipsum[1]

  \hfill
  \begin{textbox}
    \lipsum[2]  
  \end{textbox}
  \hfill

  \lipsum[3]

  \hfill
  \begin{textbox}
    \lipsum[4-5]  
  \end{textbox}
  \hfill

  \lipsum[6] 

\end{document}

Standalone of border:

\documentclass{standalone}
\usepackage{tikz}

\begin{document}
  \begin{tikzpicture}
    %\draw[help lines] (-1,-2) grid (1,2);

    \node[circle,fill=black,inner sep=1pt] at (0,2) {};
    \draw[thick] (0,2) -- (0,-2);
    \node[circle,fill=black,inner sep=1pt] at (0,-2) {};
  \end{tikzpicture}
\end{document}
  • Welcome to tex.stackexchange. Can you please provide a sample of your code as a MWE. – gman Dec 13 '18 at 17:03
  • I just added one. Keep in mind I use XeLaTeX (although it shouldn't matter). – agillbraun Dec 13 '18 at 17:20
2

You say that overlaying tikz drawings won't work, but it seems to work OK. You can use the remember and overlay options for enhanced boxes if you load the skins library.

\documentclass[10pt,oneside,twocolumn]{book}
\usepackage{lipsum}
\usepackage{tcolorbox}
\tcbuselibrary{skins}
\definecolor{textbox}{HTML}{D5D2DD}

\newtcolorbox{textbox}{
  enhanced,
  remember,
  colframe=textbox,
  colback=textbox,
  top=1mm, bottom=1mm, left=1.5mm, right=1.5mm,
  arc=0mm,
  overlay={
    \node (nw) [circle,fill=black,inner sep=1pt,xshift=4pt,yshift=-4pt]
      at (frame.north west) {};
    \node (sw) [circle,fill=black,inner sep=1pt,xshift=4pt,yshift=4pt]
      at (frame.south west) {};
    \node (ne) [circle,fill=black,inner sep=1pt,xshift=-4pt,yshift=-4pt]
      at (frame.north east) {};
    \node (se) [circle,fill=black,inner sep=1pt,xshift=-4pt,yshift=4pt]
      at (frame.south east) {};
    \draw[thick] (nw) -- (sw);
    \draw[thick] (ne) -- (se);
  }
}
\begin{document}
\raggedright

\lipsum[1]

\hfill
\begin{textbox}
  \lipsum[2]  
\end{textbox}
\hfill

\lipsum[3]

\hfill
\begin{textbox}
  \lipsum[4-5]  
\end{textbox}
\hfill

\lipsum[6] 
\end{document}

output

  • Thanks for this. What I meant by "I can't use tikz" is because I didn't want to make my class slow if I didn't have to. Tikz would definitely be slower than positioning an image (also having to load two more packages). If I find no way of doing it without Tikz, I'll use this for sure. Thanks! P.S - Why did you say "it seems to work okay"? From my test with your sample, it seems to be exactly what I want. (unfortunately, as I said, it's more of a performance issue but not a "looks" one). – agillbraun Dec 14 '18 at 1:33
  • @agillbraun Hmmm. Which step in performance are you worried about? tcolorbox is already doing its drawing with pgfso you are only adding a few extra drawing commands per box with the tikz solution. I very much doubt that tikz would be slower than positioning the (multiple) images you would need for each box. You could not draw the border, rather than setting its colour to the same as the fill to save a tiiiiiny amount of time. – David Purton Dec 14 '18 at 1:57
  • @agillbraun The difference in the worked example with and without the tikz lines is only 0.1 seconds on my 5 year old MacBook Air (with xelatex). I cannot see that you would add the images in less time. – David Purton Dec 14 '18 at 2:00
  • Hmm. That's good news I suppose. My mistake, but I didn't even think about tcolorbox using pgf. If there really isn't a huge performance hit I guess this is a good answer then! I always thought it was tikz that was slowing down my compilation, but now I'm not too sure. (I'll have to cut out "fat" in other parts of my class then). Thanks for the answer. – agillbraun Dec 14 '18 at 2:38

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.