4

In my document I use two different commands to add a picture. First I have one command that adds pictures in a table, because the title and the caption have to be with a colored background so it looks more aesthetic. I use this code for it:

\newcommand{\inputpicture}[3]{
\begin{table}[ht]
\begin{tabular}{|l|}
\hline
\parbox[0pt][2em][c]{0cm}{} \textbf{\textcolor{lightgreen}{#1}} \cellcolor{lightgreen} \hspace{12cm}    \\ \hline 
\\
\includegraphics[width=0.6\textwidth]{#3}   \\ 
\\\hline
\parbox[0pt][2em][c]{0cm}{}\footnotesize\textbf{\textcolor{lightgreen}{#2}}\cellcolor{lightgray}\\ \hline
\end{tabular}
\end{table}
}

Second command looks like this: Just a figure with a picture and no table no borders:

\newcommand{\inputpicsimple}[2]{
\begin{figure}[h] 

\includegraphics[width=0.6\textwidth]{#2}

\caption{\footnotesize\color{lightgreen}\bf #1}
\end{figure}}

Everything works perfect but the only thing that I want to change is the caption of the table. Now the caption of the \inputpicsimple adds a Figure 1, Figure 2... to every picture and i want the table to do this too, so that latex also puts Figure 1 Figure 2.. in the tables, because actually it shouldn't be a table but a picture, but when i try to use figure for the table it doesn't work and it's quite hard to change the table to a normal figure. So finally if I add two \inputpicture and one \inputpicsimple eg. then the first \inputpicture should have a "Figure 1" caption and the second \inputpicture a "Figure 2" and the \inputpicsimple a "Figure 3"....

Thanks for your help :)

1 Answer 1

2

I am pretty sure that there are other ways to achieve this special background without using cellcolor (\colorbox, for example or \tcolorbox).

If the O.P. sticks to this, \captionsetup{type=figure} is a possible way to achieve a Figure description

\documentclass{article}


\usepackage[table]{xcolor}
\usepackage[demo]{graphicx}
\usepackage{caption}


\newcommand{\inputpicture}[3]{%
\captionsetup{type=figure}%
\begin{table}[ht]
\begin{tabular}{|l|}
\hline
\parbox[0pt][2em][c]{0cm}{} \textbf{\textcolor{green}{#1}} \cellcolor{blue} \hspace{12cm}    \\ \hline 
\\
\includegraphics[width=0.6\textwidth]{#3}   \\ 
\\\hline
\parbox[0pt][2em][c]{0cm}{}\footnotesize\textbf{\textcolor{green}{#2}}\cellcolor{lightgray}\\ \hline
\end{tabular}
\end{table}
}

\newcommand{\inputpicsimple}[2]{%
\captionsetup{type=figure}
\begin{figure}[h] 

\includegraphics[width=0.6\textwidth]{#2}

\caption{\footnotesize\color{blue}\bfseries #1}
\end{figure}
}


\begin{document}

\inputpicture{Foo}{foo}{Foo again}
\caption{The fake figure}

\inputpicsimple{foo}{Foo again}


\end{document}

Update

I've switched to a tcolorbox in the background and a \inputpic command which has a starred variant for the simple display without background. The optional second argument is used for the background box, the optional 5th argument is for the lower caption box and is ignored if the starred variant is used

For the screen shot I changed the colours to make it more outstanding.

\documentclass{article}


\usepackage[dvipsnames,svgnames,x11names]{xcolor}
\usepackage[demo]{graphicx}
\usepackage{caption}

\usepackage[most]{tcolorbox}
\usepackage{xparse}

\newtcolorbox{picbackbox}[1][]{%
  nobeforeafter,
  halign=center,
  enhanced,
  sharp corners,
  colback=LightGreen,
  colbacktitle=LightGreen,
  coltitle={LightGreen},
  colframe=black,
  #1
}

\NewDocumentCommand{\inputpic}{sO{}mmO{}}{%
  \IfBooleanTF{#1}{%
    \begin{figure}[ht] 
      \centering% ???
    \includegraphics[width=0.6\textwidth]{#3}
    \caption{\footnotesize\color{LightGreen}\bfseries #4}
  \end{figure}
  }{%
    \begin{figure}[ht]
      \begin{picbackbox}[#2,width={12cm},bottomrule=0pt] % 
      \includegraphics[width=0.6\textwidth]{#4}
      \end{picbackbox}%
      \offinterlineskip

      \begin{picbackbox}[colback=LightGray,width={12cm},natural height,bottom=0pt,#5]
        \caption{\footnotesize\color{LightGreen}\bfseries #4}
      \end{picbackbox}%
    \end{figure}
  }
}

\begin{document}

\inputpic{Foo}{foo}
\inputpic*{Foo}{foo}

\inputpic{Foo}{foo}[colback=yellow]
\inputpic*{Foo}{foo}


\end{document}

enter image description here

10
  • Hey, thanks for your answer: It works, but the caption is after the table, and i want it IN the table, u know what i mean? So that the "The fake figure" is not after the table but in the last row of the table..that's just not working for me because i dont know how to combine a figure and a table
    – John
    Commented Dec 8, 2015 at 8:53
  • I'll update with an improved version
    – user31729
    Commented Dec 8, 2015 at 8:53
  • @John: You don't have a \caption command in your \inputpicture command, so I've put it outside
    – user31729
    Commented Dec 8, 2015 at 8:57
  • yeah thats the problem :D the \inputpicsimple is the one that works perfectly and has to stay as it is. just the \inputpicture command should have a \caption in it, and in the last row, but if i just copy \caption in the last row it always gives me an error, i think there is an error if u combine table and caption or something i dont know exactly..
    – John
    Commented Dec 8, 2015 at 9:01
  • @John: Why do you not write this in your question -- now I've to change. As I said, there are better methods than to use a table for making backgrounds!
    – user31729
    Commented Dec 8, 2015 at 9:03

You must log in to answer this question.

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