Draw box with tcolorbox in LaTeX – with alignment
In this tutorial, I will show you how to create nice color boxes in LaTeX with tcolorbox package.
I am in love with this package after using it. If we can use it fully and grab good hands-on with it, we can draw any kind of boxes to be used in our document.
For example:
- We can take nicely designed notes with boxes and frames.
- Create color boxes with full control and customization.
- We can also use this to show exercises.
- If you wish you can use this tcolorbox package to make question-answers document.
Yes, you can find official documentation of this package where you will be able to learn everything about this package.
Still, I thought it is worth creating a tutorial on this so that you can understand the basics of this package and how to fulfill your requirements in less time.
The first example of creating a colored box with tcolorbox in LaTeX
Through this example, I will explain the basics of it so that we can move forward to advanced parts of it.
\documentclass{article} \usepackage{tcolorbox} \begin{document} \begin{tcolorbox}[title=CodeSpeedy's Mission, colback=red!5!white, colframe=red!75!black, fonttitle=\bfseries] CodeSpeedy will keep helping to solve your LaTeX problems. Our team will be there for you to answer your queries. \end{tcolorbox} \end{document}
Output:
Now it’s time to explain what we just did.
This box has two parts.
- Title section.
- The body section.
- At first, we need to include the package in our document using
\usepackage{tcolorbox}
.
You can also use\usepackage[most]{tcolorbox}
instead of the previous command. The[most]
option loads most of the common libraries that come withtcolorbox
for convenience. \begin{tcolorbox} ..... \end{tcolorbox}
– This will create the environment for creating our box using thetcolorbox
package.- Now, we have to fill in the parameters for our box. Like adding the title, background color, frame color, fonts etc. There are a lot of other options too. But I am not going to discuss all of those here.
title=CodeSpeedy's Mission
– By looking at this, you can easily understand that this is the title that comes at the top of our box.
colback=red!5!white
– This is for our box background color. Here I have set it to light red. (5% of red mixed with 95% of white)
colframe=red!75!black
– This is for the frame background color. I suggest you play around with this by changing the values to understand how this works.
I just played with the color and changed it to green and blue. This was the output:
Alignment in tcolorbox
Here, I will show you the alignment of tcolorbox
title and body both.
Center alignment of title in tcolorbox
If you want to set the text of the title to the center in tcolorbox then you just need to use this:
title=\centering Your_title_goes_here
Let’s show you it through the full code:
\documentclass{article} \usepackage{tcolorbox} \begin{document} \begin{tcolorbox}[title=\centering I am centered title, opacityframe=0.5, colback=green!5!white, colframe=blue!75!black, fonttitle=\bfseries] tccolorbox's title is now centered \end{tcolorbox} \end{document}
Output:
Right align the tcolorbox title
You can use title=\hfill your_title_goes
here to make the title right aligned.
See the example that I have done:
\documentclass{article} \usepackage{tcolorbox} \begin{document} \begin{tcolorbox}[title=\hfill I am centered title, colback=green!5!white, colframe=blue!75!black] I am the content \end{tcolorbox} \end{document}
Output:
Align body or content text of tcolorbox to center
If you want to center the body text then just use this parameter: before upper=\centering
Example:
\documentclass{article} \usepackage{tcolorbox} \begin{document} \begin{tcolorbox}[title=\centering I am centered title, colback=green!5!white, colframe=blue!75!black, before upper=\centering] tccolorbox's title is now centered \end{tcolorbox} \end{document}
Output:
Align body text of tcolorbox to right
In this case, you just have to use: before upper=\raggedleft
Example:
\documentclass{article} \usepackage{tcolorbox} \begin{document} \begin{tcolorbox}[title=\centering I am centered title, colback=green!5!white, colframe=blue!75!black, before upper=\raggedleft] content in now right aligned \end{tcolorbox} \end{document}
Output:
Leave a Reply