[Tex/LaTex] Text in table cell not wrapped after using fix width

tables

I'm trying to make a text wrapped in a cell. I read that I can use p{width} if I want to make the text wrapped. The cell width change following the width that I set, but the text still not wrapped. Any idea?

This is my tex code :

\begin{table}
    \centering
    \caption{Contoh pesan dan topik untuk menghidupkan lampu}
    \label{tab:nyalainLampu}
    \begin{tabular}{| p{4cm} | p{10cm} |}
        \hline
        \textbf{Topik} & \texttt{sot/g/[idUser]/lampu/[idPerangkat]/on/ctl} \\
        \hline
        \textbf{Isi pesan} & \texttt{true} \\
        \hline
        \textbf{Perintah REST} & \texttt{PUT http://localhost:8080/api/LUQMANGWTA/lights/[idLokal]/state}\\
        \hline
        \textbf{Data yang dikirimkan} & \texttt{\{"on":true\}} \\
        \hline
        \textbf{Respon deCONZ} & \texttt{[\{"success":\{"/lights/[idLokal]/state/on":true\}\}]} \\
        \hline
    \end{tabular}
\end{table}

And this is the result :

enter image description here

Thank you.

Best Answer

Your table has two problems, not just one:

  • To get better line-breaking in the second column, I suggest you load the url package and replace all five instances of \texttt with \url. LaTeX will usually find pretty good line breaks for material encased in a \url statement.

  • The tabular material is too wide for the available text block (unless the sheet of paper is unusually wide and/or the page margins are unusually narrow). Instead of hard-coding the widths of the two columns of the tabular environment, I would use a tabularx environment (with width set to textwidth) and an X column type for the second column. That way, it's guaranteed that the table's width won't exceed the width of the text block.

enter image description here

\documentclass{article}
\usepackage[hyphens]{url}
\usepackage{tabularx}
\begin{document}

\begin{table}
    %%\centering
    \caption{Contoh pesan dan topik untuk menghidupkan lampu}
    \label{tab:nyalainLampu}
    \begin{tabularx}{\textwidth}{| >{\bfseries}l | X |}
        \hline
        Topik & \url{sot/g/[idUser]/lampu/[idPerangkat]/on/ctl} \\
        \hline
        Isi pesan & \url{true} \\
        \hline
        Perintah REST & \url{PUT http://localhost:8080/api/LUQMANGWTA/lights/[idLokal]/state}\\
        \hline
        Data yang dikirimkan & \url{\{"on":true\}} \\
        \hline
        Respon deCONZ & \url{[\{"success":\{"/lights/[idLokal]/state/on":true\}\}]} \\
        \hline
    \end{tabularx}

\end{table}
\end{document}

Addendum With a bit more cleaning up, and getting rid of clutter my omitting the vertical lines and most horizontal lines, one gets the following look:

enter image description here

\documentclass{article}
\usepackage[hyphens,spaces]{url}
\usepackage{tabularx,ragged2e,caption,booktabs}
\begin{document}

\begin{table}
    %%\centering
    \caption{Contoh pesan dan topik untuk menghidupkan lampu}
    \label{tab:nyalainLampu}
    \begin{tabularx}{\textwidth}{@{} >{\bfseries}l  
                     >{\RaggedRight\arraybackslash}X @{}}
        \toprule
        Topik & \url{sot/g/[idUser]/lampu/[idPerangkat]/on/ctl} \\
        \addlinespace
        Isi pesan & 
        \url{true} \\
        \addlinespace
        Perintah REST & 
        \texttt{PUT} \url{http://localhost:8080/api/LUQMANGWTA/lights/[idLokal]/state}\\
        \addlinespace
        Data yang dikirimkan & 
        \url{\{"on":true\}} \\
        \addlinespace
        Respon deCONZ & 
        \url{[\{"success":\{"/lights/[idLokal]/state/on":true\}\}]} \\
        \bottomrule
    \end{tabularx}

\end{table}
\end{document}
Related Question