1

enter image description here

Just like photo, every text is center aligned in columns, but "ADC_CH115" has different length.. so can I make it left aligned with others, and others still center aligned in columns

Another problem is, I have used \\[-0.5em] to add some vspace in Table, but it makes the line broken. How can I fix it?

The MWE for Test:

\documentclass{article}

\usepackage{array}
\usepackage{caption}
\usepackage{tabularx}

\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[h]
    \renewcommand\arraystretch{1.3} 

    \begin{tabularx}{\textwidth}{|Y|Y|Y|Y|} 
        \noalign{\hrule height 0.75pt}

        \textsf{\bfseries Funktion} & \textsf{\bfseries STM32\_Pin} & \textsf{\bfseries Pin\_Name} & \textsf{\bfseries Pin\_Funktion} \\

        \noalign{\hrule height 0.5pt}

        ADC\_CH1 & PA0 & A0 & ADC1\_IN1  \\
        ADC\_CH2 & PA1 & A1 & ADC1\_IN2  \\
        ADC\_CH3 & PA2 & A7 & ADC1\_IN3  \\
        ADC\_CH4 & PA3 & A2 & ADC1\_IN4  \\ \\[-0.5em]
        ADC\_CH6 & PA4 & A3 & ADC2\_IN1  \\
        ADC\_CH115 & PB1 & D6 & ADC1\_IN12 \\ 
        ADC\_CH7 & PA6 & A5 & ADC2\_IN3  \\
        ADC\_CH8 & PA7 & A6 & ADC2\_IN4  \\ 

        \noalign{\hrule height 0.75pt}
    \end{tabularx}

\end{table}

\end{document}
1
  • the problem with the gap can be solved like this : \\ &&&\[-0.5em] (You had to keep the column seperators even if the columns are empty.). The allign problem is somehow complicated (tex.stackexchange.com/questions/134550/…) without phantom but can be fixed by adding phantom. See my answer
    – koleygr
    Oct 21, 2017 at 15:38

2 Answers 2

1

I suggest you use \rlap to make the "excess" numerals stick out to the right without affecting the other centering operations. This methods "works" if there are only 1 or 2 "excess" numerals, which is (thankfully) the case here.

And, instead of \\ \\[-0.5em], type \\[1ex].

enter image description here

\documentclass{article}
\usepackage{caption}
\usepackage{tabularx} % 'tabularx' loads 'array' automatically
\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[h]
\renewcommand\arraystretch{1.3} 
\begin{tabularx}{\textwidth}{|Y|Y|Y|Y|} 
    \noalign{\hrule height 0.75pt} 

    \textsf{\bfseries Funktion} & 
    \textsf{\bfseries STM32\_Pin} & 
    \textsf{\bfseries Pin\_Name} & 
    \textsf{\bfseries Pin\_Funktion} \\

    \noalign{\hrule height 0.5pt}

    ADC\_CH1 & PA0 & A0 & ADC1\_IN1  \\
    ADC\_CH2 & PA1 & A1 & ADC1\_IN2  \\
    ADC\_CH3 & PA2 & A7 & ADC1\_IN3  \\
    ADC\_CH4 & PA3 & A2 & ADC1\_IN4  \\[1ex]
    ADC\_CH6 & PA4 & A3 & ADC2\_IN1  \\
    ADC\_CH1\rlap{15} & PB1 & D6 & ADC1\_IN1\rlap{2} \\ 
    ADC\_CH7 & PA6 & A5 & ADC2\_IN3  \\
    ADC\_CH8 & PA7 & A6 & ADC2\_IN4  \\ 

    \noalign{\hrule height 0.75pt}
\end{tabularx}
\end{table}
\end{document}
1
  • very cool and very easy to use, Thank you very much ^_^
    – sun0727
    Oct 21, 2017 at 16:33
1

The problem with the gap can be solved like this : \ &&&[-0.5em] (You had to keep the column separators even if the columns are empty.). The general align problem is somehow complicated without phantom or similar but here can be fixed by adding phantom:

\documentclass{article}

\usepackage{array}
\usepackage{caption}
\usepackage{tabularx}

\newcolumntype{Y}{>{\centering\arraybackslash}X}

\begin{document}

\begin{table}[h]
\renewcommand\arraystretch{1.3} 

\begin{tabularx}{\textwidth}{|Y|Y|Y|Y|} 
    \noalign{\hrule height 0.75pt}

    \textsf{\bfseries Funktion} & \textsf{\bfseries STM32\_Pin} & \textsf{\bfseries Pin\_Name} & \textsf{\bfseries Pin\_Funktion} \\

    \noalign{\hrule height 0.5pt}

    ADC\_CH1 & PA0 & A0 & ADC1\_IN1  \\
    ADC\_CH2 & PA1 & A1 & ADC1\_IN2  \\
    ADC\_CH3 & PA2 & A7 & ADC1\_IN3  \\
    ADC\_CH4 & PA3 & A2 & ADC1\_IN4  \\ &&&\\[-0.5em]
    ADC\_CH6 & PA4 & A3 & ADC2\_IN1  \\
    $\hphantom{15}$ADC\_CH115 & PB1 & D6 & $\hphantom{2}$ADC1\_IN12 \\ 
    ADC\_CH7 & PA6 & A5 & ADC2\_IN3  \\
    ADC\_CH8 & PA7 & A6 & ADC2\_IN4  \\ 

    \noalign{\hrule height 0.75pt}
\end{tabularx}

\end{table}

\end{document}

enter image description here

Second way (split the columns):

\documentclass{article}

\usepackage{array}
\usepackage{caption}
\usepackage{tabularx}

\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\newcolumntype{R}{>{\raggedleft\arraybackslash}X}
\newcolumntype{Y}{>{\centering\arraybackslash}X}
\begin{document}

\begin{table}[h]
    \renewcommand\arraystretch{1.3} 

    \begin{tabularx}{\textwidth}{|R@{\_}L|Y|Y|R@{\_}L|} 
        \noalign{\hrule height 0.75pt}

        \multicolumn{2}{|c|}{\textsf{\bfseries Funktion}} & \textsf{\bfseries STM32\_Pin} & \textsf{\bfseries Pin\_Name} 
        & \multicolumn{2}{c|}{\textsf{\bfseries Pin\_Funktion}} \\

        \noalign{\hrule height 0.5pt}

        ADC&CH1 & PA0 & A0 & ADC1&IN1  \\
        ADC&CH2 & PA1 & A1 & ADC1&IN2  \\
        ADC&CH3 & PA2 & A7 & ADC1&IN3  \\
        ADC&CH4 & PA3 & A2 & ADC1&IN4  \\ \multicolumn{2}{|c|}{}&&&\multicolumn{2}{c|}{}\\[-0.5em]
        ADC&CH6 & PA4 & A3 & ADC2&IN1  \\
        ADC&CH115 & PB1 & D6 & ADC1&IN12 \\ 
        ADC&CH7 & PA6 & A5 & ADC2&IN3  \\
        ADC&CH8 & PA7 & A6 & ADC2&IN4  \\ 

        \noalign{\hrule height 0.75pt}
    \end{tabularx}

\end{table}

\end{document}
2
  • after adding an $\hphantom{2}$ in Pin_Function column same line fixing the second alignment too
    – koleygr
    Oct 21, 2017 at 15:43
  • It works perfect for me, and very easy to use. Thank you very much
    – sun0727
    Oct 21, 2017 at 16:19

You must log in to answer this question.

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