Graphics, Figures & TablesHow to make dual language caption

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
mnajib
Posts: 6
Joined: Sat Sep 27, 2014 6:59 pm

How to make dual language caption

Post by mnajib »

Hi,

I am writing an examination question paper for school.
The paper need all in dual language.

For each sentences, I wrote English language first, and then (in italic) Bahasa Melayu usually after '\linebreak'.

I am stuck at make dual language caption as below:

Figure 1: Running Tiger
Rajah 1: Harimau sedang berlari

Below are my current preamble:

Code: Select all

\usepackage{caption}
\DeclareCaptionFormat{najibCaptionFormat}{#1#2#3#1#2#3\par} %for input format or output format?
\DeclareCaptionLabelFormat{najibCaptionLabelFormat}{\textit{#1#2}}
\captionsetup{format=najibCaptionFormat,labelformat=najibCaptionLabelFormat}
For info, I am writing in LyX.
Thank you.
Attachments
screenshot.png
screenshot.png (172.25 KiB) Viewed 8502 times

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to make dual language caption

Post by Johannes_B »

Here is a first draft of what you want (at least i think so).
The language has been set to indonesian, as i didn't know any
better.
One thing you need to be aware of, is that the optional argument
for the list entries dosn't work. If you need this, you need a
different appraoch.

Code: Select all

\documentclass{article}
\usepackage{caption}
\usepackage{xparse}
\usepackage[english,bahasa]{babel}
\makeatletter
\newcommand{\dualcap}[2]{{\selectlanguage{english}\caption{#1}}\addtocounter{\@captype}{-1}\captionsetup{labelfont=it}\caption{#2}}
\NewDocumentCommand{\encaption}{ o  m }{{\selectlanguage{english}%
\IfNoValueTF{#1}{\caption[#2]{#2}}{\caption[#1]{#2}}
}\addtocounter{\@captype}{-1}}
\makeatother
\begin{document}
\listoffigures
\listoftables
\begin{table}
	\rule{5cm}{4cm}
	\dualcap{A running tiger}{A running lion}
\end{table}
\begin{figure}
	\rule{5cm}{4cm}
	\dualcap{A running tiger}{A running lion}
\end{figure}
\begin{figure}
	\encaption{a caption in english}
	\caption[malyan list entry]{i don't speak malayan}
\end{figure}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
mnajib
Posts: 6
Joined: Sat Sep 27, 2014 6:59 pm

How to make dual language caption

Post by mnajib »

Thank you. I am amazed.

Trying to addapt with my document.
Any idea how can we remove blank-line/vertical-space between the two (English and Bahasa) caption?

to make it like:
Table 1: A running tiger
Tabel 1: Seekor harimau sedang berlari
instead of:
Table 1: A running tiger

Tabel 1: Seekor harimau sedang berlari
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to make dual language caption

Post by Johannes_B »

This empty line is the skip between the figure and the caption, which for the last caption is now the skip between the first caption the the now caption.

Here a solution for the first defined command. Look at the following, if the captions are a bit long, it might (will) look odd. If one caption is long, and one is short, it will look odd as well; but why should there be a very long caption and a very short one.

After all, it depends on how you want to do it and what you want to do.

Code: Select all

\documentclass{article}
\usepackage{caption}
\usepackage{xparse}
\usepackage[english,bahasa]{babel}
\makeatletter
\newcommand{\dualcap}[2]{{\selectlanguage{english}
\caption{#1}}\addtocounter{\@captype}{-1}\captionsetup{labelfont=it,
skip=\z@%%%%%%%%%
}\caption{#2}
}


\NewDocumentCommand{\encaption}{ o  m }{{\selectlanguage{english}%
\IfNoValueTF{#1}{\caption[#2]{#2}}{\caption[#1]{#2}}
}\addtocounter{\@captype}{-1}
}
\makeatother
\usepackage{blindtext}
\begin{document}
\listoffigures
\listoftables
\begin{table}
	\rule{5cm}{4cm}
	\dualcap{A running tiger}{A running
	lion}
\end{table}
\begin{figure}
	\rule{5cm}{4cm}
	\dualcap{A running tiger}{A running lion}
\end{figure}
\begin{figure}
	\encaption[list entry]{a caption in english. \blindtext}
	\caption{i don't speak malayan}
\end{figure}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
mnajib
Posts: 6
Joined: Sat Sep 27, 2014 6:59 pm

How to make dual language caption

Post by mnajib »

Thank you very much. I am using it :)

How can we change the word 'Figure' to 'Diagram'?

So the expected result will be something like:
Diagram 1: A running tiger
Rajah 1: Seekor harimau sedang berlari
instead of:
Figure 1: A running tiger
Gambar 1: Seekor harimau sedang berlari
Anywhere, is there any other web/site/page should I go first for refference for example to search anwser to my questions; other then google, and post questions in forums. I feel it is kind of difficult to find exact solution. As I believe will be asking too many questions :)
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to make dual language caption

Post by Johannes_B »

dd the following after loading babel

Code: Select all

\addto\captionsenglish{%
	  \renewcommand{\figurename}{Diagram}%
    }
\addto\captionsbahasa{%
	  \renewcommand{\figurename}{Tigergram}%
    }
With time comes experience, and you know where to look and what keywords you need. I can't remember the above, so i googled for babel rename figure. Just keep going on ;-)
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
mnajib
Posts: 6
Joined: Sat Sep 27, 2014 6:59 pm

How to make dual language caption

Post by mnajib »

What is the meaning of '\z@' in

Code: Select all

\captionsetup{labelfont=it,skip=\z@}
How if we need the dual language caption position side-by-side as
Diagram 1: A running tiger / Rajah 1: Seekor harimau berlari
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to make dual language caption

Post by Johannes_B »

I somehow knew this question would come.

To the first question, \p@ is a short form for point, a unit of length. \z@[\latex] is another short of 0\p@ or 0pt. But \z@ can also be just zero in some occasions. If you are in doubt, just use 0pt in your real document.

Now, a new command. Takes two mandatory arguments, the first is the caption of the english entry, the second the entry in the language that is active at that moment. Both mandatory arguments have an optional one, in case you want to put something different in the list.
By default, both captions are set underneath each other. The starred version of the command puts both side by side.

[code]


\documentclass{article}
\usepackage{caption}
\usepackage{xparse}
\usepackage{graphicx}
\usepackage[english,malay]{babel}

\addto\captionsenglish{%
\renewcommand{\figurename}{Diagram}%
}
\addto\captionsmalay{%
\renewcommand{\figurename}{Tigergram}%
}
\usepackage{blindtext}
\makeatletter%
\NewDocumentCommand{\doublecaption}{ s o m o m }{%
\IfBooleanTF#1{%
\begin{minipage}[t]{.49\textwidth}%
}{}%
{\selectlanguage{english}%
\IfNoValueTF{#2}{%
\caption{#3}%
}{%
\caption[#2]{#3}%
}%
}%
\IfBooleanTF#1{%
\end{minipage}\hfill\begin{minipage}[t]{.49\textwidth}%
}{\captionsetup{skip=2pt}}%
\captionsetup{labelfont=it}%
\addtocounter{\@captype}{-1}%
\IfNoValueTF{#4}{%
\caption{#5}%
}{\caption[#4]{#5}%
}%
\IfBooleanTF#1{\end{minipage}}{}%
}
\makeatother
\begin{document}
\listoffigures
\listoftables


\begin{figure}
\centering
\includegraphics[width=.7\textwidth]{example-image-a}
\doublecaption*{the normal english caption}[the list entry for
the foreign lang]{i gues the foreign language is your mother
tongue}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=.7\textwidth]{example-image-b}
\doublecaption[english list entry]{english caption}[other lang entry]{other lang}
\end{figure}
\begin{figure}
\centering
\includegraphics[width=.7\textwidth]{example-image-c}
\doublecaption*{no manual list entries at all}{the mandatory
arguments are used in the lists}
\end{figure}
\end{document}
[/code]
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How to make dual language caption

Post by Johannes_B »

I never had the idea to check for existing solutions till now. There are some.

First of all, there is package ccaption. To be honest, i would like my solution more.

The second i found is by the caption author and comes with some bells and whistles. Please have a look at it. Be aware, that bicaption supposes, that the first caption is the main language caption. Based on your first example, i supposed that the first caption is the english one, which i guessed is not the main language.

Code: Select all

\documentclass{article}
\usepackage{graphicx}
\usepackage[english,malay]{babel}
\usepackage{newfloat}
\DeclareFloatingEnvironment[fileext=lmf]{figureM}%
[\figurename][\listfigurename]
\usepackage[listtype+=M]{bicaption}

\addto\captionsenglish{%
	  \renewcommand{\figurename}{Diagram}%
    }
\addto\captionsmalay{%
	  \renewcommand{\figurename}{Tigergram}%
    }
\usepackage{blindtext}
\begin{document}
\listoffigures
\listoffigureMes

\begin{figure}
	\centering
	\includegraphics[width=.7\textwidth]{example-image-a}
\bicaption{the normal english caption}[the list entry for
the foreign lang]{i gues the foreign language is your mother
tongue}
\end{figure}
\begin{figure}
	\centering
	\includegraphics[width=.7\textwidth]{example-image-b}
	\bicaption{english caption}[other lang entry]{other lang}
\end{figure}
\begin{figure}
	\centering
	\includegraphics[width=.7\textwidth]{example-image-c}
\bicaption{no manual list entries at all}{the mandatory
arguments are used in the lists}
\end{figure}
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply