3

First time Latex user here

I am trying to box a text plus math using the following code

\documentclass[]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{xcolor} 
\begin{document}

\section{Section Title}

\fbox{
{\color{blue} This is a text \[x\]}
}

\boxed{
{\color{blue} This is a text \[x\]}
}

\end{document}

Unfortunately, in doing so, I received 10 errors. enter image description here

Can anyone help me debug these mere 6 lines of code?

2 Answers 2

3

Welcome to TeX.SX! The easiest way to do this is by using the framed package:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{xcolor}
\usepackage{framed}
\begin{document}

\section{Section Title}

\begin{framed}
\color{blue}This is a text \[x\]
\end{framed}

\end{document}

framed


You mentioned tcolorbox in one of your tags, so here is an alternative using the tcolorbox package:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{xcolor}
\usepackage{tcolorbox}
\begin{document}

\section{Section Title}

\begin{tcolorbox}
\color{blue}This is a text \[x\]
\end{tcolorbox}

\end{document}

tcolorbox

2

\fbox is an \hbox (LR-mode in the LaTeX book) which does not allow line breaks, and \[ is (after error checking) $$so you end up (if you scroll past errors) with

\hbox{ text $$x$$}

In in restricted hmode $$ at the primitive level so this is just the same as two $ so it is

     \hbox{ text $ empty math $ x $ empty math $}

so as always to get multi-line text into a single-line context you can use a parbox

enter image description here

\documentclass[]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{xcolor} 
\begin{document}

\section{Section Title}

\noindent\fbox{\parbox{\dimexpr\textwidth-2\fboxsep-2\fboxrule}{%
\textcolor{blue}{This is a text \[x\]}%
}}



\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .