Graphics, Figures & TablesBox with Title

Information and discussion about graphics, figures & tables in LaTeX documents.
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Box with Title

Post by wishotz »

Hello guys,
I have a small question about tikzpicture.
My aim is to create something similar to something like that

Code: Select all

--Text-------
|           |
|           |
-------------
It should be like a box without gaps of course. So I cant just use a usual fbox.

My second question is to create something like a table and in the table squares should be boxes like surveys are.

ty
Last edited by Stefan Kottwitz on Sun Jan 08, 2017 5:55 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX Beginner's Guide LaTeX Cookbook LaTeX TikZ graphics TikZによるLaTeXグラフィックス
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Box with Title

Post by Stefan Kottwitz »

You could use tcolorbox, for example:

Code: Select all

\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcolorbox{mybox}[2][]{%
  attach boxed title to top center
               = {yshift=-8pt},
  colback      = blue!5!white,
  colframe     = blue!75!black,
  fonttitle    = \bfseries,
  colbacktitle = blue!85!black,
  title        = #2,#1,
  enhanced,
}
\begin{document}
\begin{mybox}[colback=white]{Title}
This is my box.
\end{mybox}
\end{document}
box.png
box.png (3.25 KiB) Viewed 20873 times
For the other question, please open a new topic. One topic for each question is the best.

Stefan
LaTeX.org admin
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Box with Title

Post by wishotz »

Hey thanks it works great, and how do i use parameters to make the box smaller? Its right now for whole line but i want to make several small ones in one line
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Box with Title

Post by Stefan Kottwitz »

You could add a width option, such as:

\begin{mybox}[colback = white, width = 4cm]{Title}

Stefan
LaTeX.org admin
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Box with Title

Post by wishotz »

Well thats only the width of the box and what about the size of the title box? and also i want 4 such boxes next to each other with differenz sizes
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Box with Title

Post by Stefan Kottwitz »

The width of the title box is automatically adjusted to the title text width. So that should be fine. Of course you can override that. There may be a TikZ option, but a quick way I see is making the title text box itself wider, at text level:

\begin{mybox}[colback = white, width = 4cm]{\protect{\makebox[2cm][c]{Title}}}

Stefan
LaTeX.org admin
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Box with Title

Post by wishotz »

Stefan Kottwitz wrote:The width of the title box is automatically adjusted to the title text width. So that should be fine. Of course you can override that. There may be a TikZ option, but a quick way I see is making the title text box itself wider, at text level:

\begin{mybox}[colback = white, width = 4cm]{\protect{\makebox[2cm][c]{Title}}}

Stefan
Okay sounds good and how can i place 4 of these boxes next to each other?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10290
Joined: Mon Mar 10, 2008 9:44 pm

Box with Title

Post by Stefan Kottwitz »

It seems that simply putting one after the other results in the next one placed below. They seem to end the line.

What came first to my mind, is using tabular, since it can be a good way anyway to have a tabular layout.

Code: Select all

\documentclass{article}
\usepackage[most]{tcolorbox}
\newtcolorbox{mybox}[2][]{%
  attach boxed title to top center
               = {yshift=-8pt},
  colback      = blue!5!white,
  colframe     = blue!75!black,
  fonttitle    = \bfseries,
  colbacktitle = blue!85!black,
  title        = #2,#1,
  enhanced,
}
\begin{document}
\begin{tabular}{cccc}
\begin{mybox}[colback = white, width = 2cm]{Title}
First box.
\end{mybox}
&
\begin{mybox}[colback = white, width = 3cm]{Title}
This is my second box.
\end{mybox}
&
\begin{mybox}[colback = white, width = 1.8cm]{Title}
One more.
\end{mybox}
&
\begin{mybox}[colback = white, width = 2cm]{Title}
Last one.
\end{mybox}
\end{tabular}
\end{document}
boxes.png
boxes.png (8.28 KiB) Viewed 20853 times
Stefan
LaTeX.org admin
wishotz
Posts: 18
Joined: Mon Dec 26, 2016 2:39 pm

Box with Title

Post by wishotz »

Thanks
kostao
Posts: 16
Joined: Tue Jan 10, 2017 8:48 pm

Box with Title

Post by kostao »

I try to compile this code

Code: Select all

\documentclass{article}
\usepackage[most]{tcolorbox}
\usepackage{varwidth}
\usepackage{lipsum}

\newtcolorbox
{mybox}[2][]{enhanced,
before skip=2mm,after skip=2mm,
colback=black!5,colframe=black!50,boxrule=0.2mm,
attach boxed title to top left={xshift=1cm,yshift*=1mm-
\tcboxedtitleheight
},
varwidth boxed title*=-3cm,
boxed title style={frame code={
\path[fill=tcbcol@back!30!black]
([yshift=-1mm,xshift=-1mm]frame.north west)
arc[start angle=0,end angle=180,radius=1mm]
([yshift=-1mm,xshift=1mm]frame.north east)
arc[start angle=180,end angle=0,radius=1mm];
\path[left color=tcbcol@back!60!black,right color=tcbcol@back!60!black,
middle color=tcbcol@back!80!black]
([xshift=-2mm]frame.north west) -- ([xshift=2mm]frame.north east)
[rounded corners=1mm]-- ([xshift=1mm,yshift=-1mm]frame.north east)
-- (frame.south east) -- (frame.south west)
-- ([xshift=-1mm,yshift=-1mm]frame.north west)
[sharp corners]-- cycle;
},interior engine=empty,
},
fonttitle=\bfseries,
title={#2},#1}

\begin{document}
\begin{mybox}[colbacktitle=green]{My title}
\lipsum[2]
\end{mybox}
\begin{mybox}[colbacktitle=red]{My title}
\lipsum[3]
\end{mybox}
\end{document}
In overleaf.com it works good, but on my computer (TeXstudio 2.6.6, compiler=xelatex) there is error: pgfkeys: I do not know the key '/tcb/before skip' and I am going to ignore it. Perhaps you misspelled it. \begin{Newbox}[colbacktitle=green]{My title}

How fix it?
Thanks.
Post Reply